View Single Post
Old Oct 29th, 2007, 7:54 PM   #1
Adeil
Newbie
 
Join Date: Mar 2005
Location: St. Louis, USA
Posts: 3
Rep Power: 0 Adeil is on a distinguished road
Send a message via ICQ to Adeil Send a message via AIM to Adeil Send a message via MSN to Adeil Send a message via Yahoo to Adeil
Jumping to a specific line number in a text file

Hello, a friend has asked me to make a program for him, and in this program i need to read lines and interpret them from a script-type of file (by script-type, it will have text in it with commands that the program will execute, script as in typed script...kind of like what the web browser does with JavaScript.)

What i would like to know is if there is an easy way to just jump to a certain line in a text file, like line 687, for example, without having to start from the beginning and read each line to get to line 687?

If there isnt, i was planning on using something like this, but if you know a more efficient way that would be great.

c++ Syntax (Toggle Plain Text)
  1. char buffer[256];
  2. ifstream fi;
  3. fi.open(INPUT_FILE);
  4. for(int i=1; i<lineNumber; i++)
  5. {
  6. if(fi.eof())
  7. {
  8. // ran out of lines before lineNumber, handle the error with output maybe
  9. fi.close();
  10. break;
  11. }
  12. fi.getline(buffer,sizeof(buffer));
  13. }
  14. if(fi.is_open())
  15. {
  16. // the next fi.getline will have all the contents of that line
  17. fi.close();
  18. }

On a slightly different track, but on the same subject, would it be effective to load the entire text file into a multi-dimensional array, so for line 687 i could just call lines[686] (because lines[0] would be the line number 1)?
Adeil is offline   Reply With Quote