Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading last line of a file

I have a large file and I need to get only the last line from it (\n only is the line separator).
I need this to be done on iOS device, so it cannot take much memory or cpu time (like reading the whole file).
How can I do this in either Objective-C,c++ or c++11?

like image 651
Dani Avatar asked Dec 28 '22 10:12

Dani


1 Answers

Conceptually I think you'd want to open the file and seek the whole way to the end minus N bytes (maybe 80 or something). Then read that and look for the \n. If you don't find it, then seek N bytes earlier and try it on that set of N bytes, and so on until you find the \n.

As for the specific calls, that's just a matter of looking up how to open a file, seek around in it, and read data. Should be pretty straightforward. But I think the above is what you'd want to do and pick a size for N which isn't too large.

like image 151
Nerdtron Avatar answered Jan 11 '23 09:01

Nerdtron