Cant_Focus
Cant_Focus

Reputation: 17

How to read from a specific line in a file (C++)

I need to find enter a number and find the specific line in my text file to correspond with the number, and I am having a little trouble, heres what I have so far:

cout << "Please enter the ID number of the student, to view their grades: ";
cin >> number;

ifstream myfile;
myfile.open("grades.txt");
if (myfile)
{
    cout << "  ID      exam1    exam2    exam3" << endl;
    cout << "---------------------------------" << endl;

    getline(myfile, number);
    myfile >> number >> exam1 >> exam2 >> exam3;


    cout << setw(5) << number << setw(9) << exam1
        << setw(9) << exam2 << setw(9) << exam3 << endl;
    cout << "---------------------------------" << endl;

    total = exam1 + exam2 + exam3;
    cout << "TOTAL: " << setw(25) << total << endl << endl;
}
myfile.close();
return 0;

}

Upvotes: 1

Views: 75

Answers (2)

VJAYSLN
VJAYSLN

Reputation: 483

This will helps : )

#include<iostream>
#include<vector>
#include<fstream>
#include<string.h>
#include<iomanip>

using namespace std;
int main()
{
    int number;
    cout << "Please enter the ID number of the student, to view their grades: ";
    cin >> number;

    ifstream myfile;
    myfile.open("grades.txt");
    if (myfile)
    {
        string string_obj;
        string delimiter = " ";
        int id,exam1,exam2,exam3;
        size_t pos = 0;
        cout << "  ID      exam1    exam2    exam3" << endl;
        cout << "---------------------------------" << endl;
        getline(myfile, string_obj);
        //Split string into tokens
        string token[4];
        int i=0;
        while ((pos = string_obj.find(delimiter)) != string::npos)
        {
            token[i++] = string_obj.substr(0, string_obj.find(delimiter));
            string_obj.erase(0, pos + delimiter.length()); // move string for next iteration   
        }
        token[i] = string_obj.substr(0, string_obj.find(delimiter));
        id = stoi(token[0]);
        if (id == number)
        {   
            exam1 = stoi(token[1]); // convert string into int
            exam2 = stoi(token[2]);
            exam3 = stoi(token[3]);

        }


        cout << setw(5) << number << setw(9) << exam1
            << setw(9) << exam2 << setw(9) << exam3 << endl;
        cout << "---------------------------------" << endl;

        int total = exam1 + exam2 + exam3;
        cout << "TOTAL: " << setw(25) << total << endl << endl;
    }
    myfile.close();
    return 0;
}

Upvotes: 0

ABC
ABC

Reputation: 2148

Yes, you use a for loop to loop until you reach the line number, while incrementing.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  // Line #
  int line;
  // File
  std::ifstream f("_"); 
  // Text
  std::string s; 

  // Prompt
  std::cout << "Line #: " << std::endl; 
  // Store line #
  std::cin >> line; 

  // Loop, while less than line
  for (int i = 1; i <= line; i++)
    std::getline(f, s);

  // Output text at line
  std::cout << s;
  return 0;
}

Upvotes: 1

Related Questions