cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
Beginners
read() and write()
read() and write()
Apr 15, 2011 at 4:17pm UTC
Spada
(7)
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
Apr 15, 2011 at 5:55pm UTC
hamsterman
(4538)
What does it print?
Topic archived. No new replies allowed.