c++ - Not reading from file -
i want read each word file. opens file not come in while loop
string x; ifstream infile ("test.txt"); if (!infile) { cout << "unable open file"; exit(1); // terminate error } while (infile >> x) { cout << "hi" << endl; } cout << "hsiwsdsc" << endl; infile.close();
you're not checking see if file open, friend. check out code:
#include <string> int main(int argc, char** argv) { std::string line; std::ifstream input("example.txt"); if (input.is_open()) { while (input.good()) { std::getline(input, line); std::cout << line << std::endl; } } else { std::cerr << "unable open stinky file" << std::endl; } homecoming 0; }
c++
No comments:
Post a Comment