[ create a new paste ] login | about

Link: http://codepad.org/VCaAwxdO    [ raw code | output | fork ]

C++, pasted on Jan 11:
#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;
 
    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();
    
    // чтение из файла одной записи 
    ss.seekg(-24, 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;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
8
4294967272
-24
Enter x: 0
Enter y: 0

Enter x: 1
Enter y: 1

Enter x: 2
Enter y: 2

Enter x: 3
Enter y: 3

Enter x: 4
Enter y: 4

x: 2 y: 2
x: 2 y: 2


Create a new paste based on this one


Comments: