标准IO库

ifstream由istream派生而来,从文件中读取;ofstream由ostream派生而来,写到文件中去;fstream由iostream派生而来,读写文件;
标准IO库标准IO库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 }

原文链接: https://www.cnblogs.com/huanghuang/archive/2012/04/24/2468878.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/48380

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午12:13
下一篇 2023年2月9日 上午12:14

相关推荐