c++ - ifstream reading in blanks when there is data in file -
i'm trying read in data text file using c++ ifstream , reason code below doesn't work. file contains 2 numbers separated space. code doesn't print out. explain me wrong?
#include <iostream> #include <string> #include <fstream> using namespace std; void readintoadjmat(string fname) { ifstream in(fname.c_str()); string race, length; in >> race >> length; cout << race << ' ' << length << endl; in.close(); } int main(int argc, char *argv[]) { readintoadjmat("maze1.txt"); }
you should test interactions external entities successful:
std::ifstream in(fname.c_str()); std::string race, length; if (!in) { throw std::runtime_error("failed open '" + fname + "' reading"); } if (in >> race >> length) { std::cout << race << ' ' << length << '\n'; } else { std::cerr << "warning: failed read file content\n"; }
Comments
Post a Comment