簡體   English   中英

C ++順序讀取多個輸入文件

[英]C++ reading multiple input files sequentially

我有47個不同的文件:

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

在名為/ data的目錄中。 我需要將每個模板文件與目錄中的三個不同查詢文件進行比較。 這些被命名為:

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

我知道如何使所有這些都運行,但是我將不得不再剪切並粘貼這6行代碼46次,該程序將變得很長且令人困惑。

有沒有一種好的方法可以遍歷這些文件? 可能是通過循環遍歷模板文件,然后對每個模板進行三個查詢? 我顯然已經定義了相似性函數和排序函數,以及inputFile 這是我想轉換的代碼:(不是作業,這是我一直在從事的面部表情識別項目)

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);

}

使用兩個保存文件和模板名稱的數組並在它們上循環:

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

並在它們上循環:

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

然后,我將使用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);
  }
}

我認為這應該可以預期。

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