Is there any way to go to a specific line of a text file and print it to screen?
I used getline to do this, but what if i need to enter a number and the program will go to that line number and print it to the screen. is there any other way?
this is the thing inside the text file:
1. First Name
Mike
2. Last Name
Smith
3. Age
32
void listTitles(){
string titles;
ifstream fin("info.txt");
if (!fin) {
cerr << "Unable to open file";
exit(1); // call system to stop
}
getline(fin, fname, '\n');
cout << fname << endl;
getline(fin, fname, '\n');
cout << fname<< endl;
getline(fin, lname, '\n');
cout << lname<< endl;
//....
fin.close();
void displayItem(int num){
string journal;
cout << "Please enter number: ";
cin >> num;
if(num == 1){
ifstream fin("info.txt");
getline(fin, journal, '\n');
cout << journal << endl;
getline(fin, journal, '\n');
cout << journal << endl;
}
}
if i enter 1, it should display this:
1. First Name
Mike
and if i enter 2, it should display this:
2. Last Name
Smith
is getline advisable to use in this program?? it is not displaying the text i want to display.. please help me to modify my code..