2017-05-15 154 views
0

这就是我现在要做的,以便从多个txt文件中读取数据。什么会是更好的方式来处理这个问题?我想将结果存储在单独的向量中(我在这里称为out1,out2等)。 数据包含我正在进行Hough变换的XY坐标。如何从编号的txt文件读取? (data1.txt,data2.txt等)

ifstream data1, data2, data3, data4; 
vector< vector<float> > XY1, XY2, XY3, XY4; 
vector<float> row1(2), row2(2), row3(2), row4(2); 
vector<CircleData> out1, out2, out3, out4; 


// Read data 
data1.open("data1.txt"); 
while (data1 >> row1[0] >> row1[1]) 
{ 
    XY1.push_back(row1); 
} 
data1.close(); 
// Convert XY-coordinates to pixels with image size as parameter 
CoordinatesToImage convert1(XY1, 300); 
// Do Hough transformation, then search for the strongest circles in the data 
CircleHough test1; 
out1 = test1.houghPeaks(test1.houghTransform(convert1.getScan(), radius), radius, 7); 


// Next data file, same procedure 
data2.open("data2.txt"); 
while (data2 >> row2[0] >> row2[1]) 
{ 
    XY2.push_back(row2); 
} 
data2.close(); 
// Convert XY-coordinates to pixels with image size as parameter 
CoordinatesToImage convert2(XY2, 300); 
// Do Hough transformation, then search for the strongest circles in the data 
CircleHough test2; 
out2 = test2.houghPeaks(test2.houghTransform(convert2.getScan(), radius), radius, 7); 


// Next data file, same procedure 
data3.open("data3.txt"); 
...... 

回答

0

对我来说,这似乎是一个典型的代码泛化问题(所以我不会详细讨论这个具体的例子)。有很多不同的方法可以处理这个问题,但是这可以通过重构现有的代码库来实现。

正如您的代码注释中指出的那样,重复代码是负责读取和处理文件输入的部分,因此我建议先尝试通过在必要时使用变量将该部分写入其自己的子例程中。例如(在返回结果向量):

vector<CircleData> read_and_process_file_input(std::string filename) 
{ 
    // Read data 
    ifstream data (filename); 
    vector<float> row(2); 
    vector<vector<float>> XY; 

    while (data >> row[0] >> row[1]) 
    { 
     XY.push_back(row); 
    } 
    data.close(); 
    // Convert XY-coordinates to pixels with image size as parameter 
    CoordinatesToImage convert1(XY, 300); 
    // Do Hough transformation, then search for the strongest circles in the data 
    CircleHough test1; 
    return test1.houghPeaks(test1.houghTransform(convert1.getScan(), radius), radius, 7); 
} 

可以遍历这跟一个for循环(索引从1开始):

for(int i = 1; i < no_files+1; i++) 
    read_and_process_file_input("data" + i + ".txt"); 

如果你不知道输入文件的数量或者如果你想避免硬编码那些你必须添加存在检查 为每个新的输入文件。

你想如何存储结果数据很大程度上取决于应用程序;你可以使用数据结构来取代变量*。 或者(知道没有输入文件)将它们存储在一个矩阵中(每个输出数据作为列)可能会更有效率

+0

美丽。但是,在'read_and_process_file_input(“data”+ i +“.txt”);'时出现错误。 “表达式必须具有整数或无限范围的枚举类型”。我试着用一个字符串代替,并将''data“+ i +”.txt“'转换为字符串,但这并不起作用。 – asdfghjkl

+0

是的,有一个语法错误(+符号不适用于C原始字符*)。尝试使用std :: string,你是否改变了参数定义(char * filename) - >(std :: string filename)? – cadaei

+0

是的,我做到了。但是,我没有确定我的路径是哪个问题。非常感谢你!它现在有效 – asdfghjkl

1

添加方法。

void do_something(std::string file_name){ 
    ifstream data1{file_name}; 
    vector< vector<float> > XY; 
    while (data1 >> row1[0] >> row1[1]) 
    { 
     XY.push_back(row1); 
    } 
    data1.close(); 
    // Convert XY-coordinates to pixels with image size as parameter 
    CoordinatesToImage convert1(XY, 300); 
    // Do Hough transformation, then search for the strongest circles in the data 
    CircleHough test1; 
    out1 = test1.houghPeaks(test1.houghTransform(convert1.getScan(), radius), radius, 7); 
} 
+0

这可以让我落后。 Upvoted,但推荐一个快速的例子,用几个函数或循环调用。 – user4581301