I Have a Problem!

closed account (9wX36Up4)
I have searched for hours in iostream/fstream&ostream&ifstream library. Because of that:
#include<iostream>
#include<fstream>
#include<windows.h>

using namespace std;

main()
{
      int a,z;
      
      fstream file;
      file.open("Source.txt");
      if(file.is_open())
          {
          cout<<"Successful";
          for(a=1;a<=100000;a++)
          {
              file<<a;
              z=1;
              Sleep(1000);
          }
          }
      else
          cout<<"Error When Opening File"<<endl;
      file.close();
      return 0;
}

the problem is it writes 'a' as '1' first in the text then it should writes'2' in the text but it writes '12' how can i make it delete the previous number in the file and write ther new number? I used
pFile = fopen ("myfile.txt","w"); 
this code in C but i want to write it in C++ libraries and codes to learn c++. And i use DevC++
Sorry for bad english
Last edited on
Use [code][/code] tags instead of output.

As for overwriting text in a file:
Use :
file.open ("Source.txt", ios::in | ios:: out | ios::trunc)

ios::in will make the file open for input.
ios::out will make the file open for output.
ios::trunc will make the program overwrite the previous data in the file.

This is one useful link:
http://www.cplusplus.com/doc/tutorial/files/

EDIT: corrected a misplaces ".
Last edited on
closed account (9wX36Up4)
thank u Nisheeth
Topic archived. No new replies allowed.