Reading I/O text files, with seekg command

Hi,

Im having some trouble with the seekg command while using ifstream and reading textfiles. My actual code is pretty long, but i've typed out an example which gives me the same result. The "Seek Col" gives the expected output of 9. but the "seek row" doesnt print anything in the console. Can anyone see what im doing wrong here?

Any help is appreciated
Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>

using namespace std;

main(){
    ofstream SaveGet("TestFileLoc.txt");
    if (SaveGet.is_open()){
        SaveGet << 2 << endl; //row
        SaveGet << 8 << endl; //col
        SaveGet << 1 << endl; //row
        SaveGet << 9 << endl; //col
        SaveGet.close();
    }

    ifstream SaveGet2("TestFileLoc.txt");
    string seekData;
    SaveGet2.seekg(-4,ios::end);
    getline(SaveGet2, seekData);
    cout << "Seek row: " << seekData << endl; // shows nothing

    SaveGet2.seekg(-3,ios::end);
    getline(SaveGet2, seekData);
    cout << "Seek col: " << seekData << endl; // shows 9
    SaveGet2.close();

}
Topic archived. No new replies allowed.