| Код | // MS 2005 #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std;
inline void out( fstream & file ){ int n; file.seekg(0); // переходим в начало файла while(file.read((char*) &n, sizeof n)) cout << n << " "; cout << "\n\n"; file.clear(); } //////////////////// Main //////////////////////////////////////// int main(){ const int size = 20; // двадцать чисел srand(unsigned(time(NULL))); fstream file("data.dat", ios::in | ios::out | ios::trunc | ios::binary ); // делам файл
if(file.is_open()){ // если файл открыт for( int i=0; i<size; ++i ){ int n = rand()%100; file.write((char*)(&n), sizeof(int)); // запись случайных чисел в файл } if( file.eof()) file.clear(); out(file); // вывод чисел из файла на консоль
// перестановки for( int pos = 0; size/2 > pos; ++pos ){ int a, b; file.seekg(pos * sizeof a); file.read((char*)&a, sizeof a ); file.seekg((size-pos-1) * sizeof b); file.read((char*)&b, sizeof b ); file.seekp((size-pos-1) * sizeof a); file.write((char*)&a, sizeof a ); file.seekp(pos * sizeof b); file.write((char*)&b, sizeof b ); }
out(file); // вывод результата } getchar(); // пауза return 0; }
|
|