Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7650393
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: C/C++

2011-04-01 15:15:19

  1. /****************************************************************************************
  2. * 文件名:
  3. * 功能:文件处理
  4. * 说明:根据文件数据的编码方式分为文本文件和二进制文件,根据存取方式分为顺序存取文件和随机存取文件。
  5.         首先建立一个文件流对象,然后把这个流对象与实际文件相关联,称为打开文件,然后对文件流进行读写操作,最后关闭文件。
  6. * 时间:2011-3-31                                                   --Lzy
  7. *****************************************************************************************/
  8. //将信息写入a.txt

  9. #include <iostream.h>
  10. #include <fstream.h>

  11. void main()
  12. {
  13.     ofstream outfile("d:\\a.txt",ios::out);        //以写方式打开文件


  14.     if (!outfile)            
  15.         cerr<<"打开文件错误"<<endl;        //标准错误输出


  16.     else{                    
  17.         outfile<<"Lzy"<<endl;        //

  18.     }

  19. }

  20. //读a.txt信息

  21. #include <iostream.h>
  22. #include <fstream.h>

  23. void main()
  24. {
  25.     char s[5];
  26.     ifstream infile("d:\\a.txt",ios::in);        //以读方式打开文件


  27.     if (!infile)            
  28.         cerr<<"打开文件错误"<<endl;        //标准错误输出


  29.     else{    
  30.         infile.getline(s,5);            //提取一行字符

  31.         cout<<s<<'\n';

  32.         while(!infile.eof())            //文件不结束执行

  33.         {
  34.             infile.getline(s,5);            //提取一行字符

  35.             cout<<s<<'\n';
  36.         }
  37.     }
  38.     infile.close();            //关闭该文件

  39. }

  40. //文件复制

  41. #include <iostream.h>
  42. #include <fstream.h>
  43. #include <stdlib.h>

  44. void main()
  45. {
  46.     fstream infile("d:\\a.txt", ios::in | ios::out);            //以读写方式打开文件

  47.     if(!infile)
  48.     {
  49.         cerr<<"文件a.txt打开错误"<<endl;
  50.         abort();                //退出程序

  51.     }

  52.     fstream outfile("d:\\b.txt", ios::out);
  53.     if(!outfile)
  54.     {
  55.         cerr<<"文件b.txt打开错误"<<endl;
  56.         abort();                //退出程序

  57.     }

  58.     char ch;
  59.     while(infile.get(ch))            //文件流中逐个读出字符,直到文件结束

  60.         outfile.put(ch);
  61.     infile.close();
  62.     outfile.close();
  63. }


  64. /*对二进制文件进行访问,打开文件必须加上ios::binary方式*/
  65. #include <iostream.h>
  66. #include <fstream.h>
  67. #include <stdlib.h>

  68. void main()
  69. {
  70.     int data1[] = {2005, 03, 26}, data2[3];
  71.     double x = 12.45, y;

  72.     fstream outfile("d:\\ c.txt", ios::out|ios::binary);    //以写方式打开二进制文件

  73.     if(!outfile)
  74.     {
  75.         cerr<<"文件打开错误"<<endl;
  76.         abort();                //退出程序

  77.     }
  78.     
  79.     outfile.write((char *)data1, sizeof(data1));        //使用write写入信息

  80.     outfile.write((char *)&x, sizeof(x));
  81.     outfile.close();

  82.     fstream infile("d:\\ c.txt", ios::in|ios::binary);    //以读方式打开二进制文件

  83.     if(!infile)
  84.     {
  85.         cerr<<"文件打开错误"<<endl;
  86.         abort();                //退出程序

  87.     }

  88.     infile.read((char *)data2, sizeof(data2));        //使用read读信息

  89.     infile.read((char *)&y, sizeof(y));

  90.     cout<<data2[0]<<" "<<data2[1]<<" "<<data2[2]<<" "<<y<<endl;

  91. }

  92. /*文件随机访问*/
  93. #include <iostream.h>
  94. #include <fstream.h>

  95. void main()
  96. {
  97.     char c1;
  98.     ofstream outfile("d:\\f.txt", ios::out);
  99.     outfile<<"ABCDEFGHIJKLMOPQRSTUVWSYZ";
  100.     outfile.close();

  101.     ifstream infile("d:\\f.txt", ios::in);
  102.     long pos = infile.tellg();        //将文件当前指针位置赋给pos

  103.     infile.get(c1);            //提取一个字符

  104.     cout<<c1<<endl;
  105.     pos = infile.tellg();
  106.     cout<<"读取一个字符后文件指针位置:"<<pos<<endl;

  107.     infile.seekg(5);    //文件指针移到第5个字节

  108.     pos = infile.tellg();
  109.     cout<<"文件指针位置:"<<pos<<endl;

  110.     infile.get(c1);            //提取一个字符

  111.     cout<<c1<<endl;
  112.     pos = infile.tellg();
  113.     cout<<"文件指针位置:"<<pos<<endl;
  114. }
阅读(1573) | 评论(2) | 转发(2) |
给主人留下些什么吧!~~

luozhiyong1312011-06-24 08:28:49

myp2011wlgl: n长时间没看代码了,都不怎么熟了.....
写代码要多看多写

myp2011wlgl2011-06-20 14:13:42

n长时间没看代码了,都不怎么熟了