Struct in Vector, a vector include 8000 members

-5

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!

c++
vector
struct
asked on Stack Overflow Oct 3, 2014 by BenSeedGangMu

1 Answer

0

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:

  1. It doesn't run the loop once too many (because you read the last complete line and then run one more iteration because EOF is only detected when the code tries to read PAST the end of file).
  2. If data fails to be read correctly (e.g. there are letters in something that is supposed to be a number), the code stops reading data. This is perhaps not a fantastic solution, but it's better than "looping forever", which is the case if you ONLY detect EOF, because when trying to read something that is in error, the processing doesn't progress - it just stops reading at that point, and all other input values are skipped over. This tends to bring on an endless loop, because no more reading happens and EOF is never reached. I suspect this is why the 20000+ number is reached in the comment - there is an error of some sort in the input text (space in a string so that the next input is out of synch, for example).
  3. It is a little shorter/less complex (saves code-space). This is indeed a minor thing, but not a drawback by any means.

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

answered on Stack Overflow Oct 5, 2014 by Mats Petersson

User contributions licensed under CC BY-SA 3.0