Outputing data overwrites the file...

Hi coders,
I have a lil' programm which writes "lol" out to file.txt. The thing is the file is overwritten everytime the code is used. How to change the code to make "lol" add up at the end of the file everytime the code is run?

1
2
3
4
5
6
7
8
9
10
11
12
# include <stdio.h>
main ( )
{ 
FILE * myfile; 
char string[] = "lol"; 
myfile = fopen ( "file.txt", "w" ); 
fprintf (myfile, "%s!\n",string );
fclose(myfile);

return 0;
}
change this myfile = fopen ( "file.txt", "w" );

to this myfile = fopen ( "file.txt", "w", ios::app);
Error:

`ios' has not been declared
`app' undeclared (first use this function)

Am I missing some library?
#include <iosteam>
Did you meant:
#include <iostream> ?

Did include it, makes no difference...
yea sorry .. iostream, since that didnt work try #include <fstream>

Or include both ...
Last edited on
you would have to use

 
#include <fstream> 


and here:

 
myfile = fopen ( "file.txt", "w" );


you would instead put

1
2
fstream myfile;
myfile.open("file.txt", fstream::ate);


and then alter the file however you would normally. ate is a flag that makes it so everything you write will write after whatever is in the file already

edited twice for errors :P

Last edited on
Did. Still the same. Maybe something is wrong with my compiler etc.
Topic archived. No new replies allowed.