[ create a new paste ] login | about

Link: http://codepad.org/AEnQuC2g    [ 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; 
 
    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((char*)(&arr[i]), sizeof(test));
    }
    ss.clear();
    ss.seekg(0, ios::beg);
    
    
    // чтение из файла одной записи 
    ss.seekg(-3 * sizeof(tmp), ios::end);
    ss.read((char*)(&tmp), sizeof(test));
    cout << "x: " << tmp.x << " y: " << tmp.y << "\n";

	ss.seekg(2 * sizeof(tmp), ios::beg);
    ss.read((char*)(&tmp), sizeof(test));
	cout << "x: " << tmp.x << " y: " << tmp.y << "\n";
    
    ss.clear();
 
    return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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: 1 y: -1
x: 1 y: -1


Create a new paste based on this one


Comments: