I have an ASCII file (students.txt) that looks as follows (Ignore empty lines, they're an artifact of my unfamiliarity with this formatting tool):
stella 10 4.4 ...
peter 1.1 5 ...
That is, each line starts with a name, followed by one or more numbers.
The code snippet below is meant to read this file line by line, reading the name into a string and the numbers into a double, printing each of these in turn. When I run this on Ubuntu, it works fine and I get
stella 10 4.4
peter 1.1 5
However, when I run it on a Mac, I get the following:
stella 10 4.4
ter 1.1 5
When I change 'peter' to 'speter', however, it works fine...:
stella 10 4.4
speter 1.1 5
Any thoughts...?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile("students.txt");
string name;
double x;
while ( !infile.eof() ) {
infile >> name;
cout << name << ' ';
while ( infile >> x ){
cout << x << ' ';
}
cout << endl;
if (! infile.eof() )
infile.clear();
}
return 0;
}