Chinaunix首页 | 论坛 | 博客
  • 博客访问: 151312
  • 博文数量: 44
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 407
  • 用 户 组: 普通用户
  • 注册时间: 2015-11-10 13:28
个人简介

仰望星空

文章分类
文章存档

2016年(22)

2015年(22)

我的朋友

分类: C/C++

2016-03-13 14:05:38


点击(此处)折叠或打开

  1. //文件写入
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. int main(void)
  8. {
  9.     ofstream outfile("f:\\f.txt",ios::out);
  10.     if(!outfile)
  11.         cerr<<"open fiald"<<endl;
  12.     else
  13.     {
  14.         outfile<<"Lzy"<<endl;
  15.         outfile.close();
  16.     }
  17.  
  18.     return 0;
  19. }


  20. // 文件的读出
  21. #include <iostream>
  22. #include <iomanip>
  23. #include <fstream>
  24. using namespace std;
  25.  
  26. int main(void)
  27. {
  28.     char str[20];
  29.     fstream infile("f:\\f.txt",ios::in);
  30.     if(!infile)
  31.         cerr<<"open fiald"<<endl;
  32.     else
  33.     {
  34.         infile>>str;
  35.         infile.close();
  36.         cout<<str<<endl;
  37.     }
  38.  
  39.     return 0;
  40. }*/



  41. /*
  42. * 二进制文件访问
  43. */
  44. #include <iostream>
  45. #include <iomanip>
  46. #include <fstream>
  47. using namespace std;
  48.  
  49. int main(void)
  50. {
  51.     int date1[]={2011,8,9},date2[3];
  52.     double x=3.14,y;
  53.  
  54.     fstream outfile("f:\\f.txt",ios::out|ios::binary);//是否以二进制文件打开看起来也没有别的什么区别
  55.     if(!outfile)
  56.     {
  57.         cerr<<"open outfile failed"<<endl;
  58.         abort();
  59.     }
  60.     outfile.write((char *)date1,sizeof(date1));
  61.     outfile.write((char *)&x,sizeof(x));
  62.     outfile.close();
  63.  
  64.     fstream infile("f:\\f.txt",ios::in|ios::binary);
  65.     if(!infile)
  66.     {
  67.         cerr<<"open infile failed"<<endl;
  68.         abort();
  69.     }
  70.     infile.read((char *)date2,sizeof(date2));
  71.     infile.read((char *)&y,sizeof(y));
  72.     infile.close();
  73.     cout<<date2[0]<<'-'<<date2[1]<<'-'<<date2[2]<<'\t'<<y<<endl;
  74.  
  75.     return 0;
  76. }

阅读(1189) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~