急!!!!!c++文件流操作

SONGJING2009 2010-12-21 11:30:12
我想用ifstream file(“123.txt”)打开123.txt文件,"123.txt"文件中存放另一个文件的名字"111.txt"。怎么实现读取“123.txt”时获取“111.txt”的文件名,然后读取“111.txt”中的数据。
求高手解答!!!!!
...全文
75 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
gules 2010-12-21
  • 打赏
  • 举报
回复
ifstream ifs("123.txt");
string filename;
ifs >> filename; // 读取111.txt字符串
ifstream ifs2(filename.c_str()); // 打开111.txt
string data;
ifs2 >> data; // 在111.txt中读取数据
zanglengyu 2010-12-21
  • 打赏
  • 举报
回复

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s="123.txt";
vector<string> sv;
string line,filename,word;
ifstream ifile(s.c_str());
if(!ifile)
return -1;
getline(ifile,line);
istringstream istring(line);
istring>>filename;
ifile.close();
ifile.clear();
ifstream ifile1(filename.c_str());
if(!ifile1)
return -1;
system("pause");
return 0;
}
zanglengyu 2010-12-21
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s="123.txt";
vector<string> sv;
string line,filename,word;
ifstream ifile(s.c_str());
if(!ifile)
return -1;
getline(ifile,line);
istringstream istring(line);
istring>>filename;
ifile.close();
ifile.clear();
ifstream ifile1(filename.c_str());
if(!ifile1)
return -1;
system("pause");
return 0;
}
dubiousway 2010-12-21
  • 打赏
  • 举报
回复

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

int main(){
ifstream in("f:/123.txt");
char buf[128];
in>>buf;
ifstream in2(buf);
in2>>buf;
cout << buf;

in.close(), in2.close();

return 0;
}
SONGJING2009 2010-12-21
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 gules 的回复:]

ifstream ifs("123.txt");
string filename;
ifs >> filename; // 读取111.txt字符串
ifstream ifs2(filename.c_str()); // 打开111.txt
string data;
ifs2 >> data; // 在111.txt中读取数据
[/Quote]
感谢gules的解答,简单明了。
SONGJING2009 2010-12-21
  • 打赏
  • 举报
回复
谢谢,楼上各位解答。我已经明白怎么回事了。
harleypang 2010-12-21
  • 打赏
  • 举报
回复
char str=char[1024];
ifstream file1("123.txt");
file1.get(str);
ifstream file2(*str);

64,642

社区成员

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

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