codepad
[
create a new paste
]
login
|
about
Language:
C
C++
D
Haskell
Lua
OCaml
PHP
Perl
Plain Text
Python
Ruby
Scheme
Tcl
#include <iostream> #include <sstream> using namespace std; struct test { int x; int y; }; int main() { int i; const int size = 5; test arr[size]; test tmp; cout<< sizeof(tmp)<<endl; cout<<-3* sizeof(tmp)<<endl; cout<<-3*(signed)sizeof(tmp)<<endl; cout<<(unsigned)-24<<endl; for(i = 0; i < size; ++i) { cout << "Enter x: "<<(arr[i].x = i)<<endl; //cin >> arr[i].x; cout << "Enter y: "<<(arr[i].y = i)<<endl; //cin >> arr[i].y; cout << "\n"; } // запись массива в файл stringstream ss; for(i = 0; i < size; ++i) { ss.write(reinterpret_cast<char*>(&arr[i]), sizeof(test)); } ss.seekg(0, ios::beg); ss.clear(); unsigned long pos = (unsigned long) 4294967272; // чтение из файла одной записи ss.seekg(/*-3 * sizeof(tmp)*//*-24*/pos, ios::end); ss.read(reinterpret_cast<char*>(&tmp), sizeof(test)); cout << "x: " << tmp.x << " y: " << tmp.y << "\n"; ss.clear(); ss.seekg(2 * sizeof(tmp), ios::beg); ss.read(reinterpret_cast<char*>(&tmp), sizeof(test)); cout << "x: " << tmp.x << " y: " << tmp.y << "\n"; return 0; }
Private
[
?
]
Run code
Submit