C++读取文件信息并显示

zmrmary 2008-11-19 10:43:35
将信息存入到文件中以后想读出来并按行显示可以码?怎么弄?
谢谢谢谢!!!非常感谢!
...全文
224 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
朵丽儿医娘 2008-11-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <string>
using namespace std ;

void main()
{
ofstream outfile("data.txt") ;
//往文件中写入数据
outfile.close();

ifstream infile("data.txt") ;
string str ;
while( !infile.eof() )
{
getline(infile, str) ;
cout << str << endl ;
}
}
nullah 2008-11-19
  • 打赏
  • 举报
回复
排版有问题
。。。。。。。
nullah 2008-11-19
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
ofstream out("readfile.file");
out << "the first line" << endl;
out << "the second line" << endl;

out.close();

ifstream in("readfile.file");
string str;
while(getline(in, str))
{
cout << str << endl;
}

return 0;
}

霍大脚 2008-11-19
  • 打赏
  • 举报
回复
I/O
咋了啦 2008-11-19
  • 打赏
  • 举报
回复
把你写到文件的过程反过来就是读文件了,很简单的

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧