简体   繁体   English

C ++顺序读取多个输入文件

[英]C++ reading multiple input files sequentially

I have 47 different files: 我有47个不同的文件:

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

in a directory called /data . 在名为/ 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_AU01_query.dat
  • 001_AU12_query.dat 001_AU12_query.dat
  • 001_AU17_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. 我知道如何使所有这些都运行,但是我将不得不再剪切并粘贴这6行代码46次,该程序将变得很长且令人困惑。

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 . 我显然已经定义了相似性函数和排序函数,以及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);

}

Use two arrays holding the names of files and templates and loop on them: 使用两个保存文件和模板名称的数组并在它们上循环:

char* files[47] = {"file1", "file2", ...., "file47"};
char* templates[3] = {"template1", "template2", "template3"};

and loop on them: 并在它们上循环:

for(i=0; i<47; i++){
  for{j=0; j<3; j++){
    compare(file[i],template[j]);
  }
}

Then I would go with creating the file names with sprintf into the loop: 然后,我将使用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);   // create the name of the template 1-47
    sprintf(data, datas[j], i);
    compare(template, data);
  }
}

That should work as expected I think. 我认为这应该可以预期。

void work()
{


vector<float> temp;

char data[100];
char templates[100];
char* datas[3] = { "data/%03d_AU01_query.dat", "data/%03d_AU12_query.dat", "data/%03d_AU17_query.dat" };
for (int i = 1; i < 48; i++)
{
    for(int j = 0; j < 3; j++)
    {
        sprintf_s(templates, "data/%03d_template.dat", i);   // create the name of the template 1-47
        sprintf_s(data, datas[j], i);
        temp01 = similar(inputFile(data), inputFile(templates));
        sortAndOutput(temp);
    }
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM