《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统-16

来源:互联网 发布:d3.js路径 编辑:程序博客网 时间:2024/05/16 12:57

//--《C++捷径教程》读书笔记--Chapter 18--C++的I/O系统
//--Chapter 18--C++的I/O系统
//--10/16/2006 Mon.
//--Chang'an University dormitory<#1-404>
//--xwlee

//read()和write()--16
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
 int n[5]={1,2,3,4,5};
 register int i;

 ofstream out("test", ios::out|ios::binary);
 if(!out)
 {
  cout<<"cannot open file./n";
  return 1;
 }

 out.write( (char *)&n, sizeof n );
 out.close();

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

 ifstream in("test", ios::in|ios::binary );
 if(!in)
 {
  cout<<"cannot open file./n";
  return 1;
 }

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

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

 in.close();

 return 0;
}
 

原创粉丝点击