C++/FILE
Expert: vijayan - 5/2/2009
QuestionHi
i have written the code below,it works correctly,but my question is that why when i delete the line that i sign in the code,it just save in the file 'string' every time i run it,but before i deleting it for per runing of the program it saves the word 'string',for example after 3 times of runing we have three 'string',but when i delete the line after three runing we have just one 'string'?
#include<stdio.h>
FILE*fp;
void wfile();
void main(){
wfile();
}
void wfile(){
char word[10]="string",w[10];
fp=fopen("c:\\text.txt","r+");
fscanf(fp,"%s",w);//----->the line that i mentioned in my question
fprintf(fp,"%s",w);
}
Thanx
Bita
Answerfopen("c:\\text.txt","r+");
opens the file for reading and writing.
The stream is positioned at the beginning of the file.
Any output now will be written starting at the beginning of the stream.
fscanf(fp,"%s",w);//----->the line that i mentioned in my question
reads input from the file stream. The stream is positioned after the last character read.
Any output now will be written starting at the position immediately after the last character read by fscanf.