-1

I have a problem with that

going back in cursor's position by a few Lines

I've tested fseek(fp, -127, -4// back four lines) - but it didn't work could you help me please.

ABPerson
  • 489
  • 6
  • 22
Mehdi Mk
  • 1
  • 1
  • 2
    Can you show your code and explain exactly what didn't work? – Marievi Feb 14 '17 at 14:37
  • `fseek` only affects the library's view of where it thinks the next file location to read/write is. It won't affect output that's already on the screen. You can print out `'\b'` to delete characters you've already written, but when you read the beginning of a line, there's no guarantee that it'll continue to erase characters on the previous line. To do what you want dependably, you need capabilities beyond what's provided by the standard library (e.g., you might want to look into using `curses`). – Jerry Coffin Feb 14 '17 at 15:36

1 Answers1

3

First of all you don't seem to be passing the correct arguments to fseek (more specifically the last argument). Secondly, lines in text files are seldom of a fixed width, so you can't use a specific number to go back "four lines".

Instead you have to seek backward one character at a time to find the correct spot. This is not very effective using seek functions, and depending on the file you might want to either

  1. Read it all into a memory buffer where it's easy to "seek"
  2. As an alternative to the above, memory map the whole file
  3. Store the positions of all beginnings of lines so you can easily jump around
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621