#include //C++ example #include #include #include //for errno #include //for strerror using namespace std; int main(int argc, char **argv) { fstream fstr("file", ios_base::in | ios_base::out | ios_base::trunc); if (!fstr) { //if (fstr.operator!()) { cerr << "can't open file: "<< strerror(errno) << ".\n"; return EXIT_FAILURE; } fstr << "hello\n" << flush; cout << "input position == " << fstr.tellg() << "\n"; if (!fstr) { cerr << "can't tellg: " << strerror(errno) << ".\n"; return EXIT_FAILURE; } fstr.seekg(0); //rewind file back to beginning if (!fstr) { cerr << "can't seekg: " << strerror(errno) << ".\n"; return EXIT_FAILURE; } cout << "input position == " << fstr.tellg() << "\n"; if (!fstr) { cerr << "can't tellg: " << strerror(errno) << ".\n"; return EXIT_FAILURE; } char buffer[256]; //uninitialized variable if (!(fstr >> buffer)) { cerr << "can't read from file.\n"; return EXIT_FAILURE; } cout << buffer << "\n"; return EXIT_SUCCESS; }