0

What I want to do is read lines from a text file that contain two integers and remaining are strings on each line. For example, it may look something like this:

1 2 abc def pq
1 3 abc def
10 12 abc pq rt

I have till now found out how to read 2 integers and 1 string on each line but I'm really having trouble reading the whole input file as given above. Following is the code that I have tried:

#include <iostream>
#include<string>
#include<fstream>
using namespace std;

int main() {

    ifstream file( "Foodtest.txt", ios::in );
    string part1;
    int num1, num2;

    if( !file )
        cerr << "Cant open " << endl;

    while( file >> num1 >> num2 >> part1 )
    {
        cout << num1 << " " <<  num2
             << " " << part1 << endl;
    }

    file.close();
    return 0;
}
Smit Patel
  • 47
  • 7
  • 1
    Your problem statement mentions lines, but where are the lines in your code? This is classic beginner strategy, instead of basing your code around the problem you just rush in with what you know. What you should do is write a loop that reads each line (using `getline`) and then another loop that reads the numbers and strings from that line (using `istringstream` and `>>`). – john Jan 07 '23 at 07:32

0 Answers0