Unfortunately in c++, there is really no good way go locate lines in your file without reading the entire file 'character at a time.'
So, if you have to read only a single line from a file, we know we are at some point going to have to be inefficient... so let's only be inefficient once. The following code traverses the entire file once, char at a time, but saves the positions of all '\n' new lines... so any queries for specific lines from the file can be made efficiently:
#include<iostream>
#include<cctype>
#include<fstream>
#include<string>
#include<vector>
using namespace std;
int main()
{
int linecount = 0;
int linetoget = 0;
int pos = 0;
char c = '\0';
char again = '\0';
string line_str;
vector<int> linepos;
ifstream infile;
//Be sure to open in binary mode when messing about with file pointers
infile.open("C:\\Users\\Dave\\Documents\\resume.txt", ios::binary);
if(!infile.is_open())
{
cout << "\a\nError opening file!";
cin.get();
exit(1);
}
//Set first line position
linepos.push_back(0);
linecount++;
//Populate line positions from the rest of file
//This part sucks, but we only have to do it once.
do{
infile.get(c);
if(c == '\n')
{
pos = infile.tellg();
linepos.push_back(pos);
linecount++;
}
}while(infile.good());
//Reset error flags from the failing good() condition
infile.clear();
do{
do{
cout << "\nEnter line of file to get: ";
cin >> linetoget;
if(linetoget < 1 || linetoget > linecount)
{
cout << "\a\nOut of Range. Please select line number 0 to " << linecount << endl;
}
}while(linetoget < 1 || linetoget > linecount);
infile.seekg(linepos[linetoget-1], ios::beg);
getline(infile, line_str);
//clear fail bit flag if use selects to read the last line (reads and sets eof)
infile.clear();
cout << endl << line_str << endl;
cout << "\nWould you like to get another line? ";
cin >> again;
}while(toupper(again) == 'Y');
infile.close();
return 0;
}
We know we have to be very inefficient by reading the file character by character.. but at least by saving all the newline positions, it's something we only have to do once.