我在构建8000行向量时遇到了一些问题。每行是一个包含5列的结构。我不确定C ++没有响应甚至错误消息...它只是说"线程' Win32线程' (0x3b48)已退出代码-1073741510(0xc000013a)。 线程' Win32线程' (0x309c)已退出代码-1073741510(0xc000013a)。 程序' [13048] Branch Filter Vector.exe:Native'已退出代码-1073741510(0xc000013a)。"
我的代码将是
#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 :(得分:0)
发布的代码有一些&#34;损坏的位&#34;。我首先修复了“不要回到循环中”#34;并且作为一个常见的&#34;这就是你应该怎么做的#34;,将infile >> x1 >> ...
移到时间条件。这有一些好处:
我只是按照我的预期做了足够的工作,所以代码中可能仍然存在小问题。例如,从文件中读取数据应该更彻底地检查错误,例如通过读取整行然后拆分它,并对每个条目进行错误检查(与上面的第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