Erases the contents of the file test.txt. Isn't this supposed to open the file, leaving it intact and still allowing you to read and write? If not, is there another way?Code:FILE *superFile = fopen("test.txt","w+");
Erases the contents of the file test.txt. Isn't this supposed to open the file, leaving it intact and still allowing you to read and write? If not, is there another way?Code:FILE *superFile = fopen("test.txt","w+");
> leaving it intact and still allowing you to read and write?
No, that's what "a+" does
FILE *superFile = fopen("test.txt","a");
I think
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
Thought a wrote to the end of the file.
I'll try a+.
> Thought a wrote to the end of the file.
It does (initially), if you want to write elsewhere, then you'll need to fseek to it first
it's not seeking to the beginning.
It still adds to the endCode:FILE *fp = fopen("a.txt","a+"); fseek(fp,0,SEEK_SET); fputc('x',fp); fclose(fp);
Odd, it works here - gcc version 3.0.3(DJGPP)
> a.exeCode:#include <stdio.h> int main ( ) { FILE *fp = fopen( "test.txt", "a+" ); int ch; fseek( fp, 0, SEEK_SET ); ch = fgetc(fp); printf( "Ch read=%c\n", ch ); fseek( fp, 0, SEEK_SET ); fputc('@',fp); printf( "Wrote @\n" ); fseek( fp, 0, SEEK_SET ); ch = fgetc(fp); printf( "Ch read=%c\n", ch ); fclose( fp ); return 0; }
Ch read=H
Wrote @
Ch read=@
Huh. Dev-c++ sucks .
Maybe windows.h is ****ing it up?
r - Open text file for reading. The stream is positioned at the beginning of the file.
r+ - Open for reading and writing. The stream is positioned at the beginning of the file.
w - Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
w+ - Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the beginning of the file.
a - Open for writing. The file is created if it does not exist. The stream is positioned at the end of the file.
a+ - Open for reading and writing. The file is created if it does not exist. The stream is positioned at the end of the file.
(From the fopen() man page)
Jason Deckard
>r+ - Open for reading and writing. The stream is positioned at the beginning of the file.
This one sounds like it would do the trick.