Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » i can't use "fopen" function in my program

Credits go to Evert, marcin, and ReyBrujo for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
i can't use "fopen" function in my program
sofi saltana
Member #5,962
June 2005

i get problems when i try to use stdio.h "fopen" function in my program.

i just try to save information of appointments in a txt file but as far as i am concered i can't get my file opened. I am in such a hurry and i need someone to help me desperately. I will attach my "C" file here so that anyone that wants to help can see the code i'm talking about.
Is just a project wich implements an agenda that ask whether to add eliminate list the tasks or to give more or less priority to a task. every change is stored in the "tareas.txt" file. For now i'm still strugglin' to stre things in the txt file and i dont have all the options finished.

PS: Sorry if my english is not correct. I hope you understand.

CGamesPlay
Member #2,559
July 2002
avatar

if ((fichero=fopen("tareas.txt","r+t")) ==NULL)What is r+t? You want that to be "w+", I think.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

marcin
Member #5,814
May 2005

Code is good but you have not file "tareas.txt" on a disk.
r+ mean read, write, but file have to be on the disk.

PaulSiramy
Member #2,748
September 2002
avatar

If the file can't be opened, then your program tries to use allegro_message(), but allegro is not initialised yet, so I guess it crash. Move allegro_init(); to the top, and check again.

marcin
Member #5,814
May 2005

Of course allegro_init() ... ::)

sofi saltana
Member #5,962
June 2005

I have "tareas.txt" in the same folder where i have my project so there must be no problem, isn't it? correct me if i'm wrong. it could be the problem. I know that the allegro_message() is not in its place. At first i put it after allegro_init() but even then it didnt opened the file, nor executed none of the 2 allegro_message(). And that is the real problem i have THE PROGRAM DON'T EXECUTE THE "fopen" AND BOTH OF THE "allegro_message".
If somebody have some idea of this plaese help.

HoHo
Member #4,534
April 2004
avatar

Quote:

I have "tareas.txt" in the same folder where i have my project so there must be no problem, isn't it?

Where is the executeable and from where do you call it(from IDE, shortcut, ...)?

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

marcin
Member #5,814
May 2005

Put the executable and text file in the same directory.

Replace allegro_message() with:

printf("\ncan't open file\n");

And run your program from command line window.

ReyBrujo
Moderator
January 2001
avatar

Do you doubleclick the executable?

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

sofi saltana
Member #5,962
June 2005

my executable is in a folder on my desktop but i don't understand what you wanna say with from where i call it from. i just compile my program with dev-c++. is that what you are asking for?

ReyBrujo
Moderator
January 2001
avatar

Do you execute it with F5 only? If so, check inside your project folder. There must be a Debug and a Release folder. Put the text file inside each of those folders (it is possible you only have Release or Debug).

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

sofi saltana
Member #5,962
June 2005

i am not really sure of what you are saying. Would you tell me the extensión of the files you are talking about. I have a lot of files in my folder (".res","makefile.win",".layout",".exe.manifest",".rc", but some of them buy stay there from other compilations of my code).

Rampage
Member #3,035
December 2002
avatar

Put the file 'tareas.txt' in C:\. Then use

if ((fichero=fopen("C:\\tareas.txt","r+t")) ==NULL)

-R

ReyBrujo
Moderator
January 2001
avatar

You have your files in a folder, say, "C:\MyProject". When you compile with Dev-C++, it may create a folder and put the object and executable files there. So, it is possible Dev-C++ puts the executable file in "C:\MyProject\Debug". If you have your text file in "C:\MyProject", the executable won't read it. So, get absolutely sure the executable you are running is in the same directory as your text file.

Windows is a bit strange and may search for your files anywhere.

(Edited: Check this link for a proper way of finding the path to your resources).

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

miran
Member #2,407
June 2002

When you compile a .c file, you get a .exe file. Put the .exe file in the same folder as the .txt file and run it from the command prompt!

--
sig used to be here

marcin
Member #5,814
May 2005

Quote:

my executable is in a folder on my desktop

Put into this folder file tareas.txt.

Open this folder and

if you have win98:

- click start button
- click run command
- in opened "run window" type:

command

- in black window type name of your executable file

if you have winXP

- create empty text file with name "cmdline.cmd"
- in this file type:

cmd

- save and close this file
- double click on this file in folder window
- in black window type name of your executable file

CGamesPlay
Member #2,559
July 2002
avatar

marcin: Congratualations! Most redundant method of opening a program this week!

Try just:

if you have winXP

- click start button
- click run command
- in opened "run window" type:

cmd

- in black window type name of your executable file

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

marcin
Member #5,814
May 2005

Quote:

marcin: Congratualations! Most redundant method of opening a program this week!

Thank you !!! ;D

But seriously I had windows XP and if I just type "command" in "run" window, I always landed on Desktop (or MyDocuments, I don't remember exactly) (even if I opened another folder before cmd).
Only method with *.cmd files worked.
Ask Bill: why?

CGamesPlay
Member #2,559
July 2002
avatar

Do you know what "cd" does?

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

sofi saltana
Member #5,962
June 2005

i just tried your way to avoid problems with the location of the file, ReyBrujo, but i still have problems. when i try to compile it i get the "permission denied" error.
And for the first thing you said about that dev-c++ create a debug folder, its not true in my case. i have no debug folder instead i have a lot of files in the same folder and it is in this folder where i have the text file, object file and so on.

marcin
Member #5,814
May 2005

Quote:

Do you know what "cd" does?

I know but under windows TAB doesn't complete path elements (or makes it bad) (Linux has this tool).
And with *.cmd files was faster.
(Show me one person who installs windows to manages files from command line ;));

EDIT:

Quote:

..when i try to compile it i get the "permission denied" error.

Maybe you are running you program under debugger during compilation.

miran
Member #2,407
June 2002

Press ctrl+alt+delete and in the window that pops up click the processes tab, find your exe and kill it. Then try compiling again.

--
sig used to be here

sofi saltana
Member #5,962
June 2005

>Maybe you are running you program under debugger during compilation

i dont really understand what you want to say with this. i am using dev-c++ compiler and as far as i am concerned i can't compile the code while using debugger dev-c++ judt dont let you. But that dont seem to be my problem i have some lines of code that my program just dont run (see above: fopen() & allegro_message()), i even changed the allegro_message functions with printf functions but i still cant get them run.

REYBRUJO
>You have your files in a folder, say, "C:\MyProject". When you compile >with Dev-C++, it may create a folder and put the object and executable >files there. So, it is possible Dev-C++ puts the executable file >in "C:\MyProject\Debug". If you have your text file in "C:\MyProject", >the executable won't read it. So, get absolutely sure the executable >you are running is in the same directory as your text file.
>
>Windows is a bit strange and may search for your files anywhere.
>
>(Edited: Check this link for a proper way of finding the path to your >resources).

When i compile my project the compiler don't create any Debug folder i just have everything mixed-up

ReyBrujo
Moderator
January 2001
avatar

What do you have in your directory? Post the names of the files.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

CGamesPlay
Member #2,559
July 2002
avatar

I don't want to derail this thread, so this will be my last reply:

marcin: Windows XP with cmd does have tab completion of path names.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

 1   2 


Go to: