Last active
August 29, 2015 13:56
-
-
Save adituv/9291019 to your computer and use it in GitHub Desktop.
C++ space-delimited inputs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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