read() and write()

Hi guys

I am a beginner and i'm trying to deal with I/O Systems. It is still very cloudy to me and i don't undertand why the following code does,t write the number in the test.txt file.


#include<iostream>
#include<fstream>
using namespace std;

int main() {

int n[5] = {1,2,3,4,5};
int i;

ofstream out("c:/users/max/desktop/test.txt", ios::out | ios::binary);
if(!out) {
cout << "Cannot openfile";;
return 1;
}
out.write((char *) &n,sizeof n);
out.close();

for(i=0; i<5; i++)
n[i] = 0;

ifstream in("c:/users/max/desktop/test.txt", ios::in | ios::binary);
if(!in) {
cout << "Cannot openfile";
return 1;
}

in.read((char*) &n,sizeof n);

for(i=0; i<5; i++)
cout << n[i] << " ";

in.close();
return 0;
}

Can someone explain why ?

Thank you very much
What does it print?
Topic archived. No new replies allowed.