Skip to content

Instantly share code, notes, and snippets.

@adituv
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adituv/9291019 to your computer and use it in GitHub Desktop.
Save adituv/9291019 to your computer and use it in GitHub Desktop.
C++ space-delimited inputs
#include <iostream>
#include <fstream>
#include <limits>
using namespace std;
int main() {
float val1, val2;
int line = 0;
ifstream file("temp.txt", ifstream::in);
file >> val1 >> ws >> val2; // Read initial values early to combine all error checking
while(file.good()) {
cout << "Line " << line << ": " << val1 << " " << val2 << endl;
file.ignore(numeric_limits<streamsize>::max(), '\n');
line++;
file >> val1 >> ws >> val2;
}
// file.close(); // Unnecessary because RAII
return file.fail() ? 1 : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment