C++/FILE

Advertisement


Question
Hi

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

Answer
fopen("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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.