求教 C/C++ 多个文件依次读入问题

zarksoo 2011-03-28 06:25:21
求教大虾:
想要用C/C++依次读入名称类似的文件,应该怎么操作?
例如,
文件名为:data1.txt,data2.txt,……datan.txt
我想要依次读入上面的txt文件并操作,而后读入下一个文件。
...全文
202 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
FBIq 2011-03-29
  • 打赏
  • 举报
回复
#include <iostream>
#include <sstream>
int main(void)
{
std::string fileName("date");
for (int i = 1; i < 10; ++i) {
std::ostringstream ostr;
ostr << fileName << i;
std::cout << ostr.str()<< std::endl;
}
std::cin.get();
}
zarksoo 2011-03-28
  • 打赏
  • 举报
回复
明白了,以前从来没用到过,自己也想了些笨办法。
谢谢各位的指教。特别是六楼的大虾!
delphiwcdj 2011-03-28
  • 打赏
  • 举报
回复
上面改一下

#include<iostream>
#include <string>
using namespace std;
int main()
{

FILE *pFile = NULL;
string fn("data");
char num[10]="";

for (int i=1; i<10; ++i)
{
itoa(i, num, 10);
string tmp(num);
fn = "data"+tmp+".txt";// 这句
pFile = fopen(fn.c_str(),"wb+");
}

delphiwcdj 2011-03-28
  • 打赏
  • 举报
回复
上面改一下

#include<iostream>
#include <string>
using namespace std;
int main()
{

FILE *pFile = NULL;
string fn("data");
char num[10]="";

for (int i=1; i<10; ++i)
{
itoa(i, num, 10);
string tmp(num);
fn = "data"+tmp+".txt";// 这句
pFile = fopen(fn.c_str(),"wb+");
}
}
delphiwcdj 2011-03-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 zarksoo 的回复:]
求教大虾:
想要用C/C++依次读入名称类似的文件,应该怎么操作?
例如,
文件名为:data1.txt,data2.txt,……datan.txt
我想要依次读入上面的txt文件并操作,而后读入下一个文件。
[/Quote]
试试

#include<iostream>
#include <string>
using namespace std;
int main()
{

FILE *pFile = NULL;
string fn("data");
char num[10]="";

for (int i=1; i<10; ++i)
{
itoa(i, num, 10);
string tmp(num);
fn = fn+tmp+".txt";
pFile = fopen(fn.c_str(),"wb+");
}
}
孙歌 2011-03-28
  • 打赏
  • 举报
回复
按顺序遍历即可
bdmh 2011-03-28
  • 打赏
  • 举报
回复
如果你知道n是多少,也确保中间不缺数的话,可以组合文件名,然后依次打开就是了
witwolf 2011-03-28
  • 打赏
  • 举报
回复
把文件名放到一个数组中,遍历就行了

69,377

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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