请问用ifstream读文件,怎么读两次?

卩s灬戏孑x 2013-04-22 03:46:48
我想就用ifstream file连续读取两次c.txt
始终不能实现,请问难道只能定义2个file才能实现么?
代码如下:
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char c;
ifstream file;
file.open("c.txt");
while(file>>c)
{
cout<<c<<",";
}
file.close();
cout<<endl;


file.open("c.txt");
while(file>>c)
{
cout<<c<<",";
}

return 0;
}
...全文
306 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
卩s灬戏孑x 2013-04-22
  • 打赏
  • 举报
回复
引用 5 楼 rocktyt2 的回复:
引用 4 楼 happyxizi 的回复:引用 2 楼 rocktyt2 的回复:是指回到开头吗? file.seekg(0); #include <iostream> #include <fstream> using namespace std; int main() { char c; ifstream file; file.open("c.……
谢谢楼上的!可以用!
rocktyt 2013-04-22
  • 打赏
  • 举报
回复
引用 4 楼 happyxizi 的回复:
引用 2 楼 rocktyt2 的回复:是指回到开头吗? file.seekg(0); #include <iostream> #include <fstream> using namespace std; int main() { char c; ifstream file; file.open("c.txt"); while(file>>c……
加一句file.clear();试试
卩s灬戏孑x 2013-04-22
  • 打赏
  • 举报
回复
引用 2 楼 rocktyt2 的回复:
是指回到开头吗? file.seekg(0);
#include <iostream> #include <fstream> using namespace std; int main() { char c; ifstream file; file.open("c.txt"); while(file>>c) { cout<<c<<","; } // file.close(); cout<<endl; file.seekg(0); // file.open("c.txt"); while(file>>c) { cout<<c<<","; } return 0; } 这样还是不对呢……
卩s灬戏孑x 2013-04-22
  • 打赏
  • 举报
回复
引用 1 楼 Zxy0918520 的回复:
C/C++ code?123456789101112131415161718192021#include <iostream>#include <fstream>using namespace std; int main(){char c; int _INput_Value=0; ifstream file;file.open("c.txt");while(file>>c……
不对额
rocktyt 2013-04-22
  • 打赏
  • 举报
回复
是指回到开头吗? file.seekg(0);
Zxy0918520 2013-04-22
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
char c;

int _INput_Value=0;

ifstream file;
file.open("c.txt");
while(file>>c&&_INput_Value++!=1)
{
cout<<c<<",";
}
file.close();
cout<<endl;

return 0;
}
我是新手不知道这么改对不对。。。。

64,360

社区成员

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

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