Mega Code Archive

 
Categories / C++ Tutorial / File Stream
 

Check ofstream current marker position

#include <iostream> #include <fstream> using namespace std; int main(int argc, char** argv) {   ofstream fout("test.out");   fout << "12345";   long curPos = fout.tellp();   if (curPos == 5) {     cout << "Test passed: Currently at position 5" << endl;   } else {     cout << "Test failed: Not at position 5" << endl;   }   fout.seekp(2, ios_base::beg);   fout << 0;   fout.flush();   ifstream fin("test.out");   int testVal;   fin >> testVal;      if (testVal == 12045) {     cout << "Test passed: Value is 12045" << endl;   } else {     cout << "Test failed: Value is not 12045 (it was " << testVal << ")" << endl;   } }