#include #include #include using namespace std; int main() { char buffer[32]; const char* filename = "ex3-1.cpp"; ifstream fin(filename); if (!fin) { cerr << "Unable to open input file " << filename << endl; exit(1); } fin.read(buffer, 9); // Read the first 9 bytes of the file cout << '/' << buffer << '/' << endl; buffer[9] = 0; // Null terminate the buffer cout << '/' << buffer << '/' << endl << endl; cout << "fin.tellg() = " << fin.tellg() << endl; cout << "fin.peek() = " << fin.peek() << endl; cout << "static_cast(fin.peek()) = " << static_cast(fin.peek()) << endl << endl; // Reposition to byte 1 // fin.seek(1); ERROR fin.seekg(static_cast (1)); cout << "fin.tellg() = " << fin.tellg() << endl << endl; // Create a streampos object streampos pos = fin.tellg(); // pos++; ERROR // pos = pos + 5; // throws a warning pos = 2; fin >> buffer; cout << "buffer = " << buffer << endl; cout << "fin.tellg() = " << fin.tellg() << endl << endl; fin.seekg(-2, ios_base::cur); fin.read(buffer, 25); buffer[25] = 0; cout << "buffer = " << buffer << endl << endl; fin.seekg(0, ios_base::beg); fin.read(buffer, sizeof (buffer) - 1); buffer[sizeof (buffer) - 1] = 0; cout << "buffer = " << buffer << endl; }