/* * files.cpp * * Created on: 10.10.2013 * Author: trifon */ #include #include using namespace std; void testofiles() { ofstream test("test.txt", ios::app); test << "Hello, this is a text file!\n"; test << 1.2 << ' ' << 10 << endl; test.close(); int a[] = { 1, 2, 3, 5, 8, 13 }; ofstream testbin("test.dat", ios::out | ios::binary); testbin.write((const char*)a, 6 * sizeof(int)); testbin.close(); } void testifiles() { ifstream test("test.txt"); char line[100]; test.getline(line, 100); cout << "Read line: " << line << endl; double d; int i; test >> d >> i; cout << "d = " << d << endl; cout << "i = " << i << endl; test.close(); ifstream testbin("test.dat", ios::in | ios::binary); int a[6] = {0}; // !!! testbin.seekg(2); testbin.seekg(sizeof(int)); testbin.read((char*)a, sizeof(int) * 2); testbin.seekg(sizeof(int), ios::cur); testbin.read((char*)(a+2), sizeof(int) * 2); testbin.seekg(sizeof(int) * -2, ios::end); testbin.read((char*)a, sizeof(int) * 2); for(i = 0; i < 6; i++) cout << a[i] << ' '; cout << endl; testbin.close(); } void testiofile() { fstream f("essay.txt", ios::in | ios::out); char line[100]; //f.getline(line, 100); //cout << "Read line: " << line << endl; f << "IGNORE THIS TEXT!\n"; f.close(); } int main() { testofiles(); testifiles(); testiofile(); }