Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6100888
  • 博文数量: 2759
  • 博客积分: 1021
  • 博客等级: 中士
  • 技术积分: 4091
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-11 14:14
文章分类

全部博文(2759)

文章存档

2019年(1)

2017年(84)

2016年(196)

2015年(204)

2014年(636)

2013年(1176)

2012年(463)

分类:

2012-05-19 00:05:36

原文地址:C++读写文本文件 作者:escarp

#include
#include

using namespace std;

int main()
{
const char filename[] = "mytext.txt";
ofstream o_file;
ifstream i_file;
string out_text;

//写
o_file.open(filename);
for (int i = 1; i <= 10; i++)
{
o_file << "第" << i << "行\n"; //将内容写入到文本文件中
}
o_file.close();

//读
i_file.open(filename);
if (i_file.is_open())
{
while (i_file.good())
{
i_file >> out_text; //将读取的内容存储到变量out_text中
cout << out_text << endl; //在控制台输出读取的内容。为什么最后一行的内容会出现两次
}
}
else
cout << "打开文件:" << filename << " 时出错!";
i_file.close();

system("PAUSE");
return 0;
}
阅读(476) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~