I am testing the following code:
int _tmain(int argc, _TCHAR* argv[]) { int sum = 0; int x; ifstream inFile; inFile.open("test.txt"); if (!inFile) { cout << "Unable to open file"; exit(1); // terminate with error } while (inFile >> x) { cout << x << endl; } cout << "-----------------------------" << endl; // Reading from beggining file again inFile.seekg(0, ios::beg); while (inFile >> x) { cout << x << endl; } inFile.close(); return 0; }
In the above code, I want to read the file, and then move the pointer to the beginning of the file and read it again. I used inFile.seekg(0, ios::beg); to return to the beginning of the file, but it does not work? Can anybody help me? Thanks
source share