[ create a new paste ] login | about

Link: http://codepad.org/M9WwMxMo    [ 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(/*-3 * sizeof(tmp)*/4294967272, 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
cc1plus: warnings being treated as errors
Line 44: warning: this decimal constant is unsigned only in ISO C90


Create a new paste based on this one


Comments: