fseek may be unsafe?

Hello,
I tried to use fseek function in my code:
Here's the code:
1
2
3
4
5
6
7
8
9
10
char sLine[3];
FILE *fpt;
if ((fpt = fopen("file.txt", "r")) == NULL) {
    printf("Coudln't access file.txt.\n");
    exit(0);
}
fseek(fpt, -2, SEEK_END);
    

fgets(sLine, 3, fpt);

but the last two characters not always are found. At a certain point fseek no longer finds the two last characters.
Now I've changed function. I changed my code this way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ifstream inf("file.txt");

    // If we couldn't open the input file stream for reading
    if (!inf)
    {
        // Print an error and exit
        cerr << "Uh oh, file.txt could not be opened for reading!" << endl;
        exit(1);
    }

    string strData;

    

    inf.seekg(-2, ios::end); // move 15 bytes before end of file
    // Get rest of the line and print it
    getline(inf, strData);

and things seem go right.
May be the fseek function is unsafe?
Thanks in advance and goodbye.
Last edited on
Yes, off bound.
inf.seekg(-2, ios::end); // move 15 bytes before end of file
?
Topic archived. No new replies allowed.