So - Like i mentioned in the title:
I work with .txt files and the file has for example 40 000 lines. If I need only last 100 lines - do I have to go through the whole file before, or there is a simpler and better way to get instant access to the specific line?
I use Dev C++ 4.9.9.2 - using the simplest Project-->Console Aplication,
Thank you in advance.. :)
have searched "ifstream" in google? first result shows you all the functions. and if you're too lazy to do it yourself type in seekg and tellg. also heres a nice link to an article about this http://www.learncpp.com/cpp-tutorial/137-random-file-io/
If the lines have all the exact same length, it's possible to use random access to jump to any line you want. Otherwise, the file has to be read sequentially. There's simply no way of knowing how long a line is in advance. Suppose you jump ten thousand characters forward. Did you just jump a single 10k characters line, 10k single character lines, or something completely different?
What you can do, however, is use std::getline() to simplify the process.
LOL - I'm not too lazy... - But I really couldn't find it myself. However, thank you. Seekg can solve the problem. The only thing which should be done is to "prolong" lines so that all have the same length :D