2014-10-03 85 views
-4

構建8000行向量時出現了一些問題。每行是一個包含5列的結構。我不確定C++沒有任何反應,即使是錯誤信息......它只是說「線程'Win32 Thread'(0x3b48)已退出,代碼爲-1073741510(0xc000013a) 線程'Win32 Thread'(0x309c)退出代碼-1073741510(0xc000013a) 程序'[13048] Branch Filter Vector.exe:Native'已退出,代碼爲-1073741510(0xc000013a)。「向量中的結構,一個向量包含8000個成員

我的代碼將

#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <sstream> 
#include <stdio.h> 
#include <vector> 

using namespace std; 

struct branch { 
    long int FromBusNum; 
    string FromBusName; 
    double FromBusVoltage; 
    long int ToBusNum; 
    string ToBusName; 
    ; 



}; 


int main() 
{ 
    vector<branch> myBranch(8000); 
    ifstream infile; 
    long int x1; 
    string x2; 
    double x3; 
    long int x4; 
    string x5; 
    ; 

    int num = 0; // num must start at 0 

    //infile.open("Data.txt"); 
    if(infile.fail()) // checks to see if file opended 
    { 
     cout << "error" << endl; 
     return 1; // no point continuing if the file didn't open... 
    } 
    string dummyLine; //do not read in the first line 
    getline(infile, dummyLine); 

     while(!infile.eof()) // reads file to end of *file*, not line 
     { 


      myBranch.push_back(branch()); 


      infile>>x1 >> x2 >> x3 >> x4 
       >> x5 ; 

      myBranch[num].FromBusNum = x1; 
      myBranch[num].FromBusName = x2; 
      myBranch[num].FromBusVoltage = x3; 
      myBranch[num].ToBusNum = x4; 
      myBranch[num].ToBusName = x5; 

     ++num; // go to the next number 


     } 
    infile.close(); 

    ofstream fout("valency.txt"); 
    for(int i=0;i<num;i++) 
     fout/*<<myBranch[i].FromBusNum<<"\t" 
     <<myBranch[i].FromBusName<<endl; 

    fout.close(); 

    system("pause"); 
    return 0; // everything went right. 

} 

不知道問題出現在那裏......謝謝你在前進!

+0

你確定這是正確的錯誤代碼,符合微軟,0xc000013a意味着你按CTRL-C ...;) – 2014-10-03 21:58:28

+0

@MatsPetersson這是關閉彈出窗口後的錯誤消息...所以我不認爲這是由我的代碼造成的.. – BenSeedGangMu 2014-10-03 22:01:24

+0

那麼,我把你的代碼,編譯它,用8000行'1 A 3.3 8 B'做了一個文件,它工作得很好。因此無論是在你的文件中,還是在g ++和你正在使用的任何編譯器之間有所不同...... – 2014-10-03 22:04:24

回答

0

發佈的代碼有一些「壞點」。我首先解決了「不要推回去」的問題,並且作爲一種常見的「這是你應該怎麼做」,將infile >> x1 >> ...移到了while-condition。這有幾個好處:

  1. 它不會運行循環一次太多(因爲您讀了最後一個完整的行,然後再運行一個迭代,因爲只有在代碼嘗試讀取PAST結束時纔會檢測到EOF的文件)。
  2. 如果數據無法正確讀取(例如某些應該是數字的字母),代碼將停止讀取數據。這可能不是一個絕妙的解決方案,但它比「永遠循環」更好,如果你只檢測EOF,就是這種情況,因爲當試圖讀取錯誤的東西時,處理不會進行 - 它只是停止閱讀該點以及所有其他輸入值被跳過。這往往會導致無盡的循環,因爲沒有更多的閱讀發生,EOF永遠不會到達。我懷疑這就是爲什麼在註釋中達到20000+的原因 - 輸入文本中存在某種錯誤(例如字符串中的空格,以便下一個輸入不同步)。
  3. 它有點短/不那麼複雜(節省代碼空間)。這確實是一件小事,但不是任何方式的缺點。

我只做足夠的工作,因爲我期望它應該,所以在代碼中可能仍然存在小問題。例如,從文件中讀取數據應該比這更徹底地檢查錯誤,例如通過讀取整行然後拆分它,並對每個條目進行錯誤檢查(涉及上面第2點,我寫這個之後寫的句子)。如果您希望知道文件中有多少行,則可能還需要檢查num是否無法解決此問題,並在出現錯誤時將其解決。

這是我想出了代碼:

#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <sstream> 
#include <cstdio> 
#include <vector> 

using namespace std; 

struct branch { 
    long int FromBusNum; 
    string FromBusName; 
    double FromBusVoltage; 
    long int ToBusNum; 
    string ToBusName; 
}; 


int main() 
{ 
    vector<branch> myBranch(8000); 
    ifstream infile; 
    long int x1; 
    string x2; 
    double x3; 
    long int x4; 
    string x5; 

    int num = 0; // num must start at 0 

    infile.open("Data.txt"); 
    if(infile.fail()) // checks to see if file opended 
    { 
    cout << "error" << endl; 
    return 1; // no point continuing if the file didn't open... 
    } 
    string dummyLine; //do not read in the first line 
    getline(infile, dummyLine); 

    while(infile>>x1 >> x2 >> x3 >> x4 >> x5) // reads file to end of *file*, not line 
    { 
    myBranch[num].FromBusNum = x1; 
    myBranch[num].FromBusName = x2; 
    myBranch[num].FromBusVoltage = x3; 
    myBranch[num].ToBusNum = x4; 
    myBranch[num].ToBusName = x5; 

    ++num; // go to the next number 
    } 
    infile.close(); 

    ofstream fout("data.out"); 
    if (fout.fail()) 
    { 
    cout << "Error on outfile" << endl; 
    return 2; 
    } 
    for(auto v : myBranch) 
    { 
    fout << v.FromBusNum << " " 
     << v.FromBusName << " " 
     << v.FromBusVoltage << " " 
     << v.ToBusNum << " " 
     << v.ToBusName << endl; 

    } 
    cout << "num=" << num << endl; 

    return 0; // everything went right. 
} 

我跑可以在這裏找到的數據: https://gist.github.com/Leporacanthicus/1c25dd0f9b00d090f1a5