fopen for creation or modification

Status
Not open for further replies.

alzomor

Advanced Member level 2
Joined
Jun 9, 2005
Messages
674
Helped
39
Reputation
78
Reaction score
8
Trophy points
1,298
Location
Germany
Activity points
6,406
Hi

I need to open a text file .
if the file is not created I need to create it.
and if it's created I need to write within the file and not at the end


What mode should I use?

Salam
Hossam Alzomor
www(.)i-g(.)org
 

By the way which software are you using??
 

HI

we are using GNU C compiler under cygwin on Windows XP os

Salam
Hossam Alzomor
www(.)i-g(.)org
 

fopen("filename.txt","w");
 

false mode w
w Create for writing. If a file by that name already exists, it will be overwritten.

is better

Code:
      FILE *fp;
      
      fp=fopen("filename.txt","r+");  //Open an existing file for update (reading and writing).
      if(fp==NULL)  //If a file by that name NO exists!!....Create
      {
            fclose(fp);
             fp=fopen("filename.txt","w");
       }

//insert code here



//no forget
fclose(fp);
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…