免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1767 | 回复: 0
打印 上一主题 下一主题

aCC下的fstream [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-04-05 16:57 |只看该作者 |倒序浏览

使用stl,就意味着告别熟悉的c-style编程方式,对文件的处理也毫不例外.
fstream可以用来对文件进行处理,它象对待数据流一样对待文件.
以下给出一个例子.关键是事先要创建fstream.out文件,哪怕是个空文件,也要先创建.因为fstream不会为你创建.
#include
#include
int main ( )
{
         using namespace std;
          // create a bi-directional fstream object
         fstream inout("fstream.out");
          // output characters
         inout << "Das ist die rede von einem man" << endl;
         inout << "C'est l'histoire d'un home" << endl;
         inout << "This is the story of a man" << endl;
         char p[100];
          // seek back to the beginning of the file
         inout.seekg(0);
          // extract the first line
         inout.getline(p,100);
          // output the first line to stdout
         cout << endl << "Deutch :" << endl;
         cout << p;
         fstream::pos_type pos = inout.tellg();
          // extract the second line
         inout.getline(p,100);
          // output the second line to stdout
         cout << endl << "Francais :" << endl;
         cout << p;
          // extract the third line
         inout.getline(p,100);
          // output the third line to stdout
         cout << endl << "English :" << endl;
         cout << p;
          // move the put sequence before the second line
         inout.seekp(pos);
          // replace the second line
         inout << "This is the story of a man" << endl;
          // replace the third line
         inout << "C'est l'histoire d'un home";
          // replace the third line
         inout << "C'est l'histoire d'un home";
          // seek to the beginning of the file
         inout.seekg(0);
          // output the all content of the fstream
          // object to stdout
         cout << endl << endl << inout.rdbuf();
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4353/showart_19595.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP