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;
}