#include <iostream>
#include <fstream>
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;
}
阅读(1044) | 评论(0) | 转发(1) |
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
#include <fstream>
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;
}
相关热门文章
给主人留下些什么吧!~~
评论热议