0

I would like to use a FILE*, keep it's current position, make stuff, and compare it's new position to the previous one. Easy.

Eventually, I would like to continue my actions on this FILE* after an eventual relocation (move back or don't move after doing stuff). Meaning, this FILE* must continue leaving properly.

Example:

fpos_t originalPos, preBOM;
fgetpos(inFile, &originalPos);

rewind(inFile);
fgetpos(inFile, &preBOM);

unsigned char firstChar = fgetc(inFile); // Some readings...

if (originalPos != preBOM) /* Problematic comparison*/
    fsetpos(inFile, &originalPos);

Because of some advices (below), I put me in mind to use fgetpos instead of ftell. Maybe it's a mistake?

Mention to the fgetpos /vs/ ftell question: what is difference between fgetpos/fsetpos and ftell/fseek

The problem is I want to compare 2 "positions" which are in fact states (non trivial types) and have no comparison operator. And are not implemented the same on different compilers.

How can I compare originalPos to preBOM?

Sandburg
  • 757
  • 15
  • 28
  • 2
    Why do you need to compare? Can't you simply call `festpos()` unconditionally? – the busybee Aug 19 '22 at 12:34
  • It's a UCS-2 / utf-16 file meaning with a BOM at the beginning of the first line. If the file was given on pos 0, I want to continue working after the BOM, not at the input position. – Sandburg Aug 19 '22 at 12:40
  • What about `if (byte0 == 0xFE && byte1 == 0xFF) ... else if (byte0 == 0xFF && byte1 == 0xFE) ... else rewind(inFile);` ? – Ted Lyngmo Aug 19 '22 at 12:52
  • It's from my code, I left it unchanged to put something. I could cut the part `/*Example of stuff*/` and just leave a random `fgetc` to show I do things with the File in the middle. (so that the position changes) -- and functionality, it was a BOM detection. – Sandburg Aug 19 '22 at 15:34

1 Answers1

2

Quote from

The content of an fpos_t object is not meant to be read directly, but only to be used as an argument in a call to fsetpos.

That means, you can't compare fpos_t's.