C ++顺序读取多个输入文件 [英] C++ reading multiple input files sequentially

查看:222
本文介绍了C ++顺序读取多个输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有47个不同的档案:




  • 001_template.dat

  • li>
  • 047_template.dat



我需要将每个这些模板文件与三个不同的查询文件,也在目录中。这些名称为:




  • 001_AU01_query.dat

  • 001_AU12_query.dat

  • 001_AU17_query.dat。



我知道如何让所有这些运行,但我必须剪切和粘贴这6行代码46次



有没有很好的方法循环这些文件?可能通过循环遍历模板文件然后对每个模板执行三个查询?我显然有一个相似的函数和一个排序函数已经定义,以及 inputFile 。这里是我想要转换的代码:(不是家庭作业这是一个面部表情识别项目我一直在工作)

  int main()
{
vector< float> temp01;
vector< float>温度12;
vector< float> temp17;

temp01 = similar(inputFile(data / 001_AU01_query.dat),inputFile(data / 001_template.dat));
sortAndOutput(temp01);
temp12 = similar(inputFile(data / 001_AU12_query.dat),inputFile(data / 001_template.dat));
sortAndOutput(temp12);
temp17 = similar(inputFile(data / 001_AU17_query.dat),inputFile(data / 001_template.dat));
sortAndOutput(temp17);

}


解决方案

将使用sprintf创建文件名到循环中:

  char data [100]; 
char template [100];
char * datas [3] = {%3d_AU01_query.dat,%3d_AU12_query.dat,%3d_AU17_query.dat};
for(i = 0; i <47; i ++){
for {j = 0; j <3; j ++){
sprintf(template,%03d_template.dat,i); //创建模板的名称1-47
sprintf(data,datas [j],i);
compare(template,data);
}
}


I have 47 different files:

  • 001_template.dat
  • ...
  • 047_template.dat

in a directory called /data. I need to compare each of these template files to three different query files, also in the directory. These are named:

  • 001_AU01_query.dat
  • 001_AU12_query.dat
  • 001_AU17_query.dat.

I know how to get all of this to run, but I will have to cut and paste these 6 lines of code 46 more times and the program will get very long and confusing.

Is there a good way to loop over these files? Possibly by looping over the template files and then doing three queries for every template? I obviously have a similarity function and a sort function already defined, as well as inputFile. Here is the code I would like to convert: (not homework this is for a facial expression recognition project I have been working on)

int main()
{
vector<float> temp01;
vector<float> temp12;
vector<float> temp17;

temp01 = similar(inputFile("data/001_AU01_query.dat"), inputFile("data/001_template.dat"));
sortAndOutput(temp01);
temp12 = similar(inputFile("data/001_AU12_query.dat"), inputFile("data/001_template.dat"));
sortAndOutput(temp12);
temp17 = similar(inputFile("data/001_AU17_query.dat"), inputFile("data/001_template.dat"));
sortAndOutput(temp17);

}

解决方案

Then I would go with creating the file names with sprintf into the loop:

char data[100];
char template[100];
char* datas[3] = {"%3d_AU01_query.dat", "%3d_AU12_query.dat", "%3d_AU17_query.dat"};
for(i=0; i<47; i++){
  for{j=0; j<3; j++){
    sprintf(template, "%03d_template.dat", i);   // create the name of the template 1-47
    sprintf(data, datas[j], i);
    compare(template, data);
  }
}

That should work as expected I think.

这篇关于C ++顺序读取多个输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆