2012-03-17 41 views
0

我建立一个神经网络,其中我需要从一个CSV文件中读取数据并在每行存储作为网络输入一个单独的训练集。每行包含一组模式并以目标输出结束。故障读取和加载数据文件在C++

我已经能够读取并解析所述第一线到其各自的指针列表,然后通过创建接受两个指针的基类的一个新对象它们在载体中存储。

问题是,我无法弄清楚如何从文件中读取一行,将其存储在对象指针向量中,然后继续读取每行直到文件结束,同时存储向量的每一行。我将不胜感激,可能与可行的建议,可能是更有效或优化提供沿任何帮助。

void processLine(string &line) 
{ 

     double* pattern = new double[numPatterns]; 
     double* target = new double[numTargets]; 


     char* cstr = new char[line.size()+1]; 
     char* t; 
     strcpy(cstr, line.c_str()); 

     int i = 0; 
     t=strtok (cstr,","); 

     while (t!=NULL && i < (numPatterns + numTargets)) 
     { 
     if (i < numPatterns) pattern[i] = atof(t); 
     else target[i - numPatterns] = atof(t); 

     //move token onwards 
     t = strtok(NULL,","); 
     i++;    
    } 


    data.push_back(new priceEntries(pattern, target));  
} 


bool loadDataFile(const char* file, int numP, numT) 
{ 
     numPatterns = numP; 
    numTargets = numT; 

    fstream inputFile; 
    inputFile.open(filename, ios::in); 


    if (inputFile.is_open()) 
    { 

     string line = ""; 

     //read data 
     while (getline(inputFile, line)) 
     {  
      processLine(line); 
     } 

    random_shuffle(data.begin(), data.end()); 

    inputFile.close() 

    return true; 
    } 

    else 
    { 
     cout << "File could not be opened.\n"; 
    } 
} 
+0

请张贴你为了得到帮助有代码。 – 2012-03-17 21:46:39

+0

请检查您的代码。你至少错过了一个大括号。并请修复缩进。 – Bart 2012-03-17 22:29:02

回答

0

你的问题似乎是基本的CSV解析。如果是的话,我建议使用一个简单的解析库(libcs​​v想到的),而不是试图处理从头解析。