I have some problem building a 8000 rows vector. Each row is a struct including 5 columns. I am not sure what the C++ have no response even error message... it just says "The thread 'Win32 Thread' (0x3b48) has exited with code -1073741510 (0xc000013a). The thread 'Win32 Thread' (0x309c) has exited with code -1073741510 (0xc000013a). The program '[13048] Branch Filter Vector.exe: Native' has exited with code -1073741510 (0xc000013a)."
My code will be
#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.
}
Not sure where the problem appears... Thank you in advance!
The posted code has some "broken bits". I first fixed up the "don't push back in the loop", and as a common "this is how you should do it", moved the infile >> x1 >> ...
into the while-condition. This has a few benefits:
I have only made enough to make it work as I expect it should, so there may still be small problems in the code. For example, reading data from a file should check more thoroughly for errors than this does, for example by reading an entire line and then splitting it, with error checking for each entry (relates to point 2 above, that I wrote after I wrote this sentence). If you expect to know how many lines there are in a file, you may also want to CHECK that num
doesn't got over this, and error out if it does.
This is the code I came up with:
#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.
}
The data I run can be found here: https://gist.github.com/Leporacanthicus/1c25dd0f9b00d090f1a5
User contributions licensed under CC BY-SA 3.0