fopen to create if not exists and update if exists

Feb 19, 2010 at 4:21pm
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.
Feb 19, 2010 at 4:34pm
fopen("yourfile.ext","wb");

- read+write
- binary
- erases file if it already exists
- creates file if it doesn't exist

EDIT: or did you not want to erase?
Last edited on Feb 19, 2010 at 4:35pm
Feb 19, 2010 at 4:35pm
need to update if file exists, not erase file.
Feb 19, 2010 at 8:39pm
In this case you ll have to make 2 calls to fopen, one to check if the file exists and one to open it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <conio.h>

int main()
{

    FILE *file;
    int file_exists;
    const char * 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;
}


You might also wanna take a look here for more info about fopen:

http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx
Last edited on Feb 19, 2010 at 8:46pm
Feb 19, 2010 at 9:30pm
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?
Last edited on Feb 19, 2010 at 9:33pm
Feb 19, 2010 at 10:00pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*...*/

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

/*...*/
Last edited on Feb 19, 2010 at 10:15pm
Feb 19, 2010 at 10:20pm
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.
Feb 19, 2010 at 10:57pm
You do not need to open it twice.

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");.
Feb 19, 2010 at 11:05pm
but he also wants to be able to read... this doesn't give him this privilege...
Feb 19, 2010 at 11:12pm
fopen(file, "a+");
Feb 19, 2010 at 11:21pm
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.


the above can be found in http://msdn.microsoft.com/en-us/library/yeby3zcb%28VS.71%29.aspx

so, using "a" or "a+" won't allow him to modify any data, only append. and he clearly states that he wants to ->
update if file exists, begin at start of file
Last edited on Feb 19, 2010 at 11:24pm
Feb 20, 2010 at 12:15am
I need to be able to update contents.

Here's my usage if it'll help...

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.
Feb 20, 2010 at 1:45am
@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.
Topic archived. No new replies allowed.