Reputation: 32
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;
}
Upvotes: 0
Views: 690
Reputation: 598039
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;
}
Upvotes: 1
Reputation: 7100
Replace
inFile >> x >> y >> z;
getline(inFile, name);
By
inFile >> x >> y >> z;
inFile.ignore();
getline(inFile, name);
Because
And for a depth explanation
Why does std::getline() skip input after a formatted extraction?
Upvotes: 1
Reputation: 11018
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);
Upvotes: 1