File I/O - displaying output properly
hi,
I'm trying to find out how I go about properly displaying data from a file like this;
Code:
#include <sys/stat.h>
struct stat results;
#include <fstream>
#include <iostream>
using namespace std;
class Data
{
public:
int iInt1;
};
int main()
{
Data xData;
xData.iInt1 = 3;
char *filename = "Reading&WritingComplexData1.txt";
fstream file (filename, ios::out|ios::in);
file.write((char*)&xData, sizeof(xData));
if (stat(filename, &results) == 0)
results.st_size;
int size = results.st_size;
if( file.is_open())
{
cout<<"file is open"<<endl;
char *memblock = new char[size];
file.seekg(0,ios::beg);
file.read(memblock, size);
file>>memblock;
cout<<memblock<<endl; //trouble with this part, displaying the data properly?
}
cin.get();
}
If I only use 1 or 2 char members it displays the file as is, but soon as I use anything more (int, string etc) it changes. I'm guessing some special handling/conversion must take place somewhere at some point, I just don't know how/where.
Most exercises/examples I've come across don't really go into the how really, with cases like this (i.e classes). Just wondering if someone could provide a workable example or (practical,not theory) material that helps explain this.
Thanks!