I was confused because you are opening the same file as before.
The issue is because your code does not match the file format. Your file format appears to be sets of 3 lines where the first two lines are strings and the third is a number. You should read input like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
if(std::ifstream in ("CWC_Master.txt"))
{
std::string line1, line2, line3;
while(std::getline(in, line1) && std::getline(in, line2) && std::getline(in, line3))
{
//line1 and line2 are ready for processing, but we need to extract the number from line 3
std::istringstream iss (line3); //#include <sstream>
double num;
iss >> num;
//now num has the number in it
std::cout << line1 << " " << line2 << " " << num << std::endl;
}
}
else
{
std::cerr << "Unable to open file." << std::endl;
}
I'm a little confused about your code, if Im to be honest. Im sure its standard stuff, but I fresh into c++.
What alterations to my code could read the line by line? I also dont think I understand what you are saying about the file formatting is wrong. Because the getline should get the line no matter what the data type is, correct?
And also, im outputting numbers and strings. just not line by line. I feel like im missing one line of code somewhere..
while(fin >> x && !fin.eof())
{
fin.get(ch);
getline(fin, x);
count++;
cout << x << " " << endl;
}
You first try to read a single word from the file, then you try to read an entire line into the same variable. Then you print it. This doesn't match the file content at all.