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.
char buffer[256];
ifstream fi;
fi.open(INPUT_FILE);
for(int i=1; i<lineNumber; i++)
{
if(fi.eof())
{
// ran out of lines before lineNumber, handle the error with output maybe
fi.close();
break;
}
fi.getline(buffer,sizeof(buffer));
}
if(fi.is_open())
{
// the next fi.getline will have all the contents of that line
fi.close();
}
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)?