2014-10-03 82 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