Im writing an app where I need to open a file, read data, check a element of the struct to see if my data already exists. if exists I update if not I append to end.
everything is working except if my file does not exist. I need fopen flags for:
read/write
binary
update if file exists, begin at start of file
create file if not exists
ive tried a variety of flags but cant figure it out.
#include <stdio.h>
#include <conio.h>
int main()
{
FILE *file;
int file_exists;
constchar * filename="my_file.txt";
/*first check if the file exists...*/
file=fopen(filename,"r");
if (file==NULL) file_exists=0;
else {file_exists=1; fclose(file);}
/*...then open it in the appropriate way*/
if (file_exists==1)
{
printf("file exists!\n");
file=fopen(filename,"r+b");
}
else
{
printf("file does not exist!\n");
file=fopen(filename,"w+b");
}
if (file!=NULL)
{
printf ("file opened succesfully!\n");
fclose(file);
}
getch();
return 0;
}
I did read somewhere on the web about the 2 call method but was hoping it could be accomplished with just flags for the sake of cleanliness. Thanks for the replies.
one question, will it cause problems calling fopen twice without an fclose in between?
no, I don't think so. you can for example, open two files, read from one, write to the other and then close them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*...*/
/*define the file handlers*/
FILE * file1, file2;
/*open your files*/
file1=fopen("input.dat","r");
file2=fopen("output.dat","w");
/*
here read from the first using fscanf(file1,...);
and write to the other using fprintf(file2,...);
*/
/*close both of them when you're done*/
fclose(file1);
fclose(file2);
/*...*/
but it would be best to close an open file handler before using it to open another file if that's what you mean.
/*...*/
FILE * file;
file=fopen("file1.txt","w");
/*do some writing on file1.txt*/
fclose(file); /*close it when you're done*/
/*if you omit this fclose call, I don't think that there will be
a problem in calling the fopen afterwards, BUT there is a case
that the data you wrote to file1.txt will not be saved! And
then you'll lose the file handler, since you ll associate it with a
new file and you'll never be able to close the first file...*/
file=fopen("file2.txt","w")
/*do some more writing on file2.txt*/
fclose(file); /*close this one too*/
/*...*/
I misread your first example. I see now two opens are not called without a close. it was the fopen where the file does not exist where fopen was being called without an fclose first which makes sense.
You want the file to be updated if it already exists (so you don't lose it's contents) or created if it doesn't exist, yes? You don't need to open it twice. Open the file in append mode: fopen(file, "a");.
When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.
This is for a game plugin where when a player disconnects I am saving statistical info (kills,deaths,etc). If it is his first visit I will just append struct data to the end of file. If he has been to the server before, his previous data will be over-written. Everything is working perfectly until I tested it without a file created. This is why I need the file to be create if not exist and be able to over-write data if it does exist (without deleting existing entries!). I don't mind using the double fopen() method but would like to use the proper flags if available.
@m4ster r0shi,
fair enough. Personally, I don't use windows, so "a+" isn't avaliable to me anyway.
@OP,
In this case, yes, you'll have to use two fopen() calls. You don't need to close it the first time*, but really, you should just have two variables so you can read and write it as much as you like,
*sometimes you don't have to close the file at all. But you'll probably get a memory leak, because IIRC a FILE* is a dynamically allocated pointer to a data structure typedef'd as FILE. When you allocate it with fopen() it gets pointed to an instance of FILE on the heap, and when you fclose() it, it gets free()d.