1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
int main()
11 {
12 fstream data;
13 const char name[10] = "hello";
14 int buffer[10] = {0,1,2,3,4,5,6,7,8,9};
15 int buf[10];
16 int ret = openFile(data,name);//call the function to open file
17 if(!ret)
18 {
19 exit(-1);
20 }
21 data.write((char*) buffer, sizeof(buffer));//write to file
22 cout<<"write completed"<<endl;
23 //data.close();
24
25 //data.open(name,ios::in | ios::binary);
26 data.read((char *) buf, sizeof buf);//read from file and store it in buf
27
28 for(int i=0;i<10;i++)
29 {
30 cout<<buf[i]<<" ";//printing values in buf
31 }
32 cout<<endl;
33
34 data.close();
35 return 0;
36 }
37
38 //function to just open the file
39 int openFile(fstream &datafile,const char *ptr)
40 {
41 datafile.open(ptr, ios::in | ios::out | ios::binary);
42 if(!datafile)
43 {
44 cout<<"open failed!"<<endl;
45 return 0;
46 }
47 return 1;
48 }
|