0

I am trying to move one position back in the stdin.Using this code:

fpos_t fPos;
fgetpos(stdin,&fPos);
fPos -= 1;
fsetpos(stdin,&fPos);

But I get this error:

 invalid operands to binary expression ('fpos_t'(aka '_G_fpos_t') and 'int')

But my question is why I get it? Then how can I set the fPos to one position behind?

Thanks.

1 Answers1

3

fpos_t is a structure, not an int, and it's only meant to be used in fgetpos and fsetpos calls - you're not supposed to manipulate it directly

Use fseek() with SEEK_CUR argument to manipulate the current stream position

Maksim Satsikau
  • 1,444
  • 11
  • 11
  • @KhaledMohammad That's because interactive streams (which `stdin` typically is) are not seekable. As noted by another comment, you can use `ungetc` to go back one character (but only one character). – Raymond Chen Oct 29 '14 at 13:34
  • @Raymond Chen Bro but the code works completely fine in Windows(CODEBLOCKS) but I can't make it work in linux I just get that error. – Khaled Mohammad Oct 29 '14 at 19:23
  • @KhaledMohammad `fpos_t` is an opaque type. Whether it is an integral type or a structure is implementation-dependent. Doing math on `fpos_t` is not guaranteed to work. – Raymond Chen Oct 30 '14 at 14:11