0

I just started to refresh myself on the <fstream> library in C++, and I'm attempting to store the first line of my text file into 3 integer variables, all split with spaces. The second line of the text file has a string and I'm trying to get my string variable to store this. However, I'm unsure as to how to go to the next line of the file. The file looks like this:

5 10 15
My name is Luke

I'm aware of using getline to get the whole line and then go to the next one, but I'm not storing the first line into one variable, but 3 so I can't use getline() for that one. Here's my code.

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

int main(int argc, char **argv)
{
    ifstream inFile;
    ofstream outFile;

    string name;
    int x,y,z;

    outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    outFile << 5 << " " << 10 << " " << 15 << endl << "My name is Luke";
    outFile.close();

    inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    inFile >> x >> y >> z;
    getline(inFile, name);
    cout << x  << " " << y << " " << z << " " << endl << name;

    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620

3 Answers3

1

Replace

inFile >> x >> y >> z;
getline(inFile, name);

By

inFile >> x >> y >> z;
inFile.ignore();
getline(inFile, name);

Because

Why is cin.ignore() necessary when using "getline" after "cin", but is not required when using "cin" multiple times?

And for a depth explanation

Why does std::getline() skip input after a formatted extraction?

asmmo
  • 6,409
  • 1
  • 7
  • 22
1

std::getline reads until it encounters the delimiter or end of file, which here is the newline right after

5 10 15\n
        ^
// you need to ignore this

You can ignore it by

inFile.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
getline(inFile, name);
Andreas DM
  • 9,654
  • 6
  • 31
  • 58
1

You have a couple of choices:

You can read the 3 integers using operator>>, then ignore() the ifstream until a new line is skipped, then you can use std::getline() to read the second line:

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

int main(int argc, char **argv)
{
    ifstream inFile;
    ofstream outFile;

    string name;
    int x,y,z;

    outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    outFile << 5 << " " << 10 << " " << 15 << "\n" << "My name is Luke";
    outFile.close();

    inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    inFile >> x >> y >> z;
    infile.ignore(numeric_limits<streamsize>::max(), '\n');
    getline(inFile, name);
    cout << x  << " " << y << " " << z << " " << endl << name;

    return 0;
}

Otherwise, despite your claim that "I can't use getline() for [the 1st line]", you CAN actually use std::getline() to read the 1st line. Simply use a std::istringstream afterwards to read the integers from that line. Then you can use std::getline() to read the 2nd line:

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

int main(int argc, char **argv)
{
    ifstream inFile;
    ofstream outFile;

    string line, name;
    int x,y,z;

    outFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    outFile << 5 << " " << 10 << " " << 15 << "\n" << "My name is Luke";
    outFile.close();

    inFile.open("C:\\Users\\luked\\Desktop\\Test.txt");
    getline(inFile, line);
    istringstream(line) >> x >> y >> z;
    getline(inFile, name);
    cout << x  << " " << y << " " << z << " " << endl << name;

    return 0;
}
Remy Lebeau
  • 454,445
  • 28
  • 366
  • 620
  • Ah okay I see what you mean by using getline for the first line. I was just unsure as to how to read it into each var separately. – Luke Duggan Jan 17 '20 at 21:09