i can't use "fopen" function in my program
sofi saltana

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

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

marcin

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

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

Of course allegro_init() ... ::)

sofi saltana

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
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, ...)?

marcin

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

Do you doubleclick the executable?

sofi saltana

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

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).

sofi saltana

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

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

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

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).

miran

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!

marcin
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

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

marcin
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

Do you know what "cd" does?

sofi saltana

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
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

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.

sofi saltana

>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

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

CGamesPlay

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.

marcin
Quote:

... doesn't complete path elements (or makes it bad)

If you have it try use it. Some XPs (new or with updates) has it but it works terrible.

gillius

Tab completion for Windows XP works for me (but it doesn't work identical to Linux! It is different but it works as designed). It also exists in Win2000, with a registry "hack"

marcin
Quote:

Tab completion for Windows XP works for me ...

OK, I have habits from Linux.
If it doesn't complete name of a file or a dir after I press TAB it doesn't work for me. :)

EDIT:

(Habits are the second nature of Human.)

Quote:

but it works as designed

Everything works as designed... ;D
(Only often designers can't predict some additional features...)

sofi saltana

the files i have in my folder are:
-agenda.c
-agenda.dev
-agenda.exe.Manifest
-agenda.exe
-agenda.layout
-agenda.o
-agenda_private.h
-agenda_private.rc
-agenda_private.res
-makefile.win
-teraes.txt
-6 bitmaps(they are just bitmaps used as buttons)

(note that this folder contains all the files that were generated from other compilations)

HoHo

-teraes.txt vs if ((fichero=fopen("tareas.txt","r+t")) ==NULL)
Is that a typo?
Does this program work if you execute the program directly(doubleclick on
it in explorer)?

marcin
Quote:

Is that a typo?

Good work Sherlock HoHolmes !!! :)

(But I said at start that he has not tareas.txt) ;D

sofi saltana

Marcin >(But I said at start that he has not tareas.txt)

i do have tareas.txt in the same folder i have my executable. but i still can't get this file opened. Please try to help.

marcin
Quote:

Please try to help.

Did you run executable from command line window and with printf(...) in code?

EDIT:

Can you compile this example:

1#include <stdio.h>
2#include <stdlib.h>
3 
4 
5int main(void)
6{
7 FILE *fichero;
8 
9 if ((fichero=fopen("tareas.txt","r+t")) ==NULL)
10 {
11 
12 printf("\nError al abrir fichero.\n");
13 exit(EXIT_SUCCESS);
14 }
15
16 fprintf(fichero, "%s", "asdf");
17 fclose(fichero);
18 
19 return 0;
20}

And run it.

sofi saltana

yes i did but my program never execute the code lines that suppose to open file and report if the file is opened. And as you said i tried with allegro_message and printf but i get the same result

marcin

And what about the code from my previous post (the EDIT section)?

Quote:

... my program never execute the code lines that suppose to open file and report if the file is opened.

But if it's not opened you should obtain message.

EDIT:

I just have compiled you code.

I run it and the file is opened OK.

But there is another problem. GUI is opened and closed immediately.

And this appear after:

do_dialog(the_dialog, -1);

sofi saltana

i compiled it and it gives no problem (it worxed). I already tried this earlier.
When i saw that it worked i wondered if the problem was in the allegro way to execute porgram, or something with allegro anyway; thats why in the code i attached in my first question the fopen was put before allegro_init.
even then it didn't worked so i got run out of ideas.

EDIT:

i cant get it working in my computer y would like to get the code of the open file to ensure how is the code that works(because i changed my code many times, maybe it will work even in my computer).
About the dialog it is supposed to open the dialog the_dialog and when this dialog opens, the user have to choose if he wants to add a new task. If he choose to add, the aniadir function will change a variable that will make switch enter the dialog aniadirtarea. But "the_dialog" dialog should not close automatically.

marcin

Can you replace your code with this:

if ((fichero=fopen("tareas.txt","r+t")) ==NULL){

 printf("\nError al abrir fichero.\n");

}

else

printf("\nfichero ABIERTO.\n");

And run it from command line.

Read EDIT in my previous post about problem with the GUI.

EDIT:

I have question:

In Allegro 4.0.3

allegro_message()

works without set_gfx_mode() ?

EDIT:

(If do you use 4.0.3 version of course)

Evert
Quote:

In Allegro 4.0.3

allegro_message()

works without set_gfx_mode() ?

Yes. Actually, you should only use it if you're not in graphics mode.

sofi saltana

ok! you really gave me the key to the problem i have. The problem is a big fault i made at creating the txt file. Unconsciously i created a tareas.txt.txt file, thats why i couldn't get my file opened, because i never called it.
As Marcin said, and Evert confirmed (thank you both for the help), allegro_message don't work in graphics mode. That's why i had the impression that those line of code never got run.
When Marcin told me that my code worked i had the confirmation that there was something wrong with me.

i'm really glade to have found these place. thank you all of you speccially MARCIN & REYBRUJO both of them helped me trough lots of messages.

marcin
Quote:

As Marcin said, and Evert confirmed (thank you both for the help), allegro_message don't work in graphics mode.

I'm only asked that it works without graphics mode.
(I'm newbie in Allegro)
(But path was good)

Evert said that we shouldn't use it in gfx mode.

:)

Thread #503241. Printed from Allegro.cc