How can i select the last line of a text file using C
c, file-io
Solution
No direct way, but my preferred method is:
- Go to the end of the file
- Read last X bytes
- If they contain '\n' - you got your line - read from that offset to the end of the file
- Read X bytes before them
- back to 3 until match found
- If reached the beginning of the file - the whole file is the last line
Problem
I am trying to find out a way to select the last line of a text file using C (not c++ or c#, just C) and I am having a difficult time finding a way to do this, if anyone could assist me with this problem I would be very grateful, thanks! (btw for a good example of what i am trying to do, this would be similar what to tail -n 1 would be doing in bash)