2013-03-11 35 views
-2

我使用C++,我從文件系這樣讀:從文件讀入一個載體

D x1 x2 x3 y1 

我的代碼有:

struct gate { 
    char name; 
    vector <string> inputs; 
    string output; 
}; 

main功能:

vector <gate> eco; 

int c=0; 
int n=0; 
int x = line.length(); 
while(netlist[c][0]) 
{ 
    eco.push_back(gate()); 
    eco[n].name = netlist[c][0]; 
    eco[n].output[0] = netlist[c][x-2]; 
    eco[n].output[1] = netlist[c][x-1]; 
} 

其中netlist是我已將該文件複製到的二維數組。

我需要幫助來循環輸入並將它們保存在向量eco中。

+5

:您應使用此代碼:

ifstream somefile(path); vector<gate> eco; gate g; while (somefile >> g) eco.push_back(g); // or, simpler, requiring #include <iterator> vector<gate> eco(std::istream_iterator<gate>(somefile), std::istream_iterator<gate>()); 

和過載operator >>適當地爲您gate類型? – jrok 2013-03-11 14:07:06

+0

你在哪裏讀書? – 2013-03-11 14:11:06

+1

這很混亂,代碼不完整。目前尚不清楚你想要完成什麼或爲什麼。 – Omnifarious 2013-03-11 14:11:23

回答

3

我不完全理解二維數組的感覺,但我懷疑它是多餘的。你爲什麼不直接讀入,而不是載體

std::istream& operator >>(std::istream& in, gate& value) { 
    // Error checking … return as soon as a failure is encountered. 
    if (not (in >> gate.name)) 
     return in; 

    gate.inputs.resize(3); 
    return in >> gate.inputs[0] >> 
       gate.inputs[1] >> 
       gate.inputs[2] >> 
       gate.output; 
} 
+0

@Jerry這很諷刺。我更驚訝地發現兩個人沒有意識到我發佈了大量的垃圾*。 – 2013-03-11 15:52:51

+0

我想,這還不夠公平。 – 2013-03-11 15:54:19

相關問題