• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
一蓑烟雨
C/C++,Linux,语音技术
博客园    首页    新随笔    联系   管理    订阅  订阅
标准IO库

ifstream由istream派生而来,从文件中读取;ofstream由ostream派生而来,写到文件中去;fstream由iostream派生而来,读写文件;

IO
 1 #include "stdafx.h"
 2 #include <fstream>
 3 #include <iostream>
 4 #include <string>
 5 using namespace std;
 6 int _tmain(int argc, _TCHAR* argv[])
 7 {
 8     ifstream infile;
 9     ofstream outfile;
10     string filename = "D:/Program Files/C++_CODE/streamtest/Debug/201204.txt";//注意路径
11     string print;
12     infile.open(filename.c_str());//打开文件
13     if(!infile) //检查是否打开
14     {
15      cout<<"open file is error!"<<endl;
16      return -1;
17     }
18     else
19     {
20         while(!infile.eof())
21      {
22         getline(infile,print);//读内容
23         cout<<"Today I want to say : "<<print.c_str()<<endl;
24      }
25      infile.close(); //关闭文件
26      infile.clear(); //清除流状态
27     }
28     string rfilename="D:/Program Files/C++_CODE/streamtest/Debug/201204_R.txt";
29     outfile.open(rfilename.c_str());
30     if(!outfile)
31     {
32      cout<<"open file is error!"<<endl;
33      return -1;
34     }
35     else
36     {
37      outfile<<"I hate you!";//往打开的文件中写内容
38      outfile.close();
39     }
40     return 0;
41 }
posted on 2012-04-24 21:13  lovemu  阅读(248)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3