// File iosflags.cpp #include #include #include #include using namespace std; int main() { char x; // Print IO status flags. cout << "IO Status Flags" << endl; cout << hex << setw(4) << ios::badbit << " "; cout << hex << setw(4) << ios::failbit << " "; cout << hex << setw(4) << ios::eofbit << " "; cout << hex << setw(4) << ios::goodbit << endl << endl; // Input file is in project folder. fstream fin; fin.open("testfile.txt", ios::in); // Check if open was successful. cout << boolalpha << fin.fail() << endl; // Check state flags. cout << fin.rdstate() << endl; // Set base back to decimal. cout << dec; // Show file output when opened as text file. cout << endl; cout << "File when opened as text." << endl; while((x = fin.get()) != EOF) { cout << x << " "; cout << setw(3) << right << static_cast(x) << " "; } cout << endl; // Close file so we can reopen later. fin.close(); // Clear eof flag so we can reopen file. fin.clear(); // Open file as binary. fin.open("testfile.txt", ios::in | ios::binary); // Repeat file output when opened as binary. cout << endl; cout << "File when opened as binary." << endl; while((x = fin.get()) != EOF) { cout << x << " "; cout << setw(3) << right << static_cast(x) << " "; } cout << endl << endl; // Print file mode flags. cout << "File Mode Flags" << endl; cout << hex << setw(4) << ios::app << " "; cout << hex << setw(4) << ios::ate << " "; cout << hex << setw(4) << ios::binary << " "; cout << hex << setw(4) << ios::in << " "; cout << hex << setw(4) << ios::out << " "; cout << hex << setw(4) << ios::trunc << endl << endl; // Print seek offset flags. cout << "Seek Offset Flags" << endl; cout << hex << setw(4) << ios::end << " "; cout << hex << setw(4) << ios::cur << " "; cout << hex << setw(4) << ios::beg << endl << endl; return EXIT_SUCCESS; } // Output: // IO Status Flags // 4 2 1 0 // // false // 0 // // File when opened as text. // T 84 h 104 i 105 s 115 32 i 105 s 115 // 10 a 97 32 t 116 e 101 s 115 t 116 . 46 // 10 // File when opened as binary. // 13 h 104 i 105 s 115 32 i 105 s 115 // 13 a 97 32 t 116 e 101 s 115 t 116 . 46 // 10 // File Mode Flags // 8 4 20 1 2 10 // // Seek Offset Flags // 2 1 0