Skip to content

Instantly share code, notes, and snippets.

@JiaChangGit
Created April 24, 2024 04:09
Show Gist options
  • Save JiaChangGit/8f5d7f40578fbb2530e6fa110352e557 to your computer and use it in GitHub Desktop.
Save JiaChangGit/8f5d7f40578fbb2530e6fa110352e557 to your computer and use it in GitHub Desktop.
111-cpp_io-ifstreamTest
#include <fstream>
#include <iostream>
using namespace std;
int main() {
ifstream in1;
in1.open("ifstreamTest1.txt");
if (in1.fail()) {
cout << "file opening is failed...\n";
exit(1);
}
while (!in1.eof()) {
int value = 0;
in1 >> value;
cout << value << "\n";
cout << "good()=" << in1.good() << ",";
cout << "fail()=" << in1.fail() << ",";
cout << "bad()=" << in1.bad() << ",";
cout << "eof()=" << in1.eof() << "\n";
if (in1.good() == false) {
break;
}
}
in1.close();
in1.open("ifstreamTest2.txt");
if (in1.fail()) {
cout << "file opening is failed...\n";
exit(1);
}
while (!in1.eof()) {
int value = 0;
in1 >> value;
cout << value << "\n";
cout << "good()=" << in1.good() << ",";
cout << "fail()=" << in1.fail() << ",";
cout << "bad()=" << in1.bad() << ",";
cout << "eof()=" << in1.eof() << "\n";
if (in1.good() == false) {
break;
}
}
in1.close();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment