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..

Unless the lines are formatted to have exactly the same length, getline() is really the best way to do what you want, even if you throw away most of the lines:

for (int x = 0; getline(fs, line) && x < selectedLine; ++x)
{
    // all work in condition
}

if (!fs)
{
    cerr << "could not load requested line\n";
}

but some dont have the same length, and what if i added a new line of text?

If you just want to add a new line of text at the end of the file, then open the file for append mode. Read about the open method here. Or call seekp() to set the file pointer to end of file before writing.

If you want to add a new line somewhere else then you have to rewrite the entire file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.