0

I am trying to read information from a .txt file that looks as shown below.To read the first 2 lines of integers I use the ">>" operator to read them into an array. My problem is that I want to read the next line(in its entirety) to a string so I can convert it into a stream and parse it, however when I try to simply use getline it doesn't actually read anything into the string which got me thinking that the cursor hasn't actually moved to the next line and I was wondering how to do this or any other method that would serve the save purpose. The structure of the txt file is below:

2

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
765DEF 01:01:05:59 enter 17
ABC123 01:01:06:01 enter 17
765DEF 01:01:07:00 exit 95
ABC123 01:01:08:03 exit 95

My code is show below:

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



int main()
{
    int arr[24];
    int milemarker;
    int numberofCases;


    ifstream File;
    File.open("input.txt");
    File >> numberofCases;

    for (int i = 0; i < 24; i++)
    {
        File >> arr[i];
    }

    for (int i = 0; i < 24; i++)
    { 
        cout << arr[i] << " ";
    }
    cout << endl;
    string line;
    getline(File, line);
    cout << line;


    system("pause");
}
KoolaidLips
  • 247
  • 1
  • 8
  • 20

1 Answers1

2

I think you miss getline() call:

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



int main()
{
    int arr[24];
    int milemarker;
    int numberofCases;


    ifstream File;
    File.open("input.txt");
    File >> numberofCases;

    for (int i = 0; i < 24; i++)
    {
        File >> arr[i];
    }

    for (int i = 0; i < 24; i++)
    { 
        cout << arr[i] << " ";
    }
    cout << endl;
    string line;
    getline(File, line);
    getline(File, line);
    cout << line;


    system("pause");
}

Operator >> reads tokens between delimiters. By default, space and new line are delimiters. So after last operator >> call in first loop, you still on the same line, and first getline() call reads only new line character.

gomons
  • 1,946
  • 14
  • 25