0

I am trying to read a dat file into a vector the data is 5 in a row and 400 columns however, it results in cygwin_exception::open_stackdumpfile: Dumping stack trace to ....

#include <iostream> 
#include <stdio.h>
#include <fstream>
#include <vector>
using namespace std; 

int main() { 

char line[40];
vector<double> vec[400];
#define DEMENSION 5
ifstream file ("train.dat", ios::in);


//file.open("train.dat",ios::in);

int i=0,l=1;
double tmp;

 while(!file.eof()){
    //vec.push_back(l);
    vec[i].push_back(l);

    for(int j = 0; j < DEMENSION; j++) {
        file >> tmp;
        vec[i].push_back(tmp);
    }
    cout << vec[i].size()<< endl;
    i++;

} 
cout<<i<<endl;

//file.close();
for(int iy; iy < 400; ++iy){
    vector<double>:: iterator iter = vec[iy].begin();
    for(int ix = 0; iter != vec[iy].end(); ++iter, ++ix){
         cout << *iter<<" " ;
        }
        cout<<endl;
 }



 return 0; 
}

But if I change the dimension to 6, it works. Then the vector results in 7 number in a row(one number is add by me),left only 344 columns, which is not I want....It supposed to be 6 numbers if the dimension is correctly set at 5,

mccneedle
  • 31
  • 1
  • 3
    Note that you don't create a single vector of 400 elements, you create an *array of 400 vectors!* Is that really your purpose? Please see e.g. [this `std::vector` reference](http://en.cppreference.com/w/cpp/container/vector) (especially the [constructor reference](http://en.cppreference.com/w/cpp/container/vector/vector)) for more information. Also note that by calling `push_back` you *append* to the vector, you do not overwrite existing elements. – Some programmer dude Apr 12 '17 at 14:25
  • 1
    And *if* you want an array of vectors, why not create a vector *of vectors*? That would make it much more dynamic, and you could have more (or less) than 400. – Some programmer dude Apr 12 '17 at 14:28
  • 2
    Lastly please take some time to read [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Apr 12 '17 at 14:28
  • What if you have dis file and not dat file? – user4581301 Apr 12 '17 at 15:41
  • Thanks! eof is the main problem for my code. Still fixing some minor issue. – mccneedle Apr 12 '17 at 15:55
  • I'm wondering why are you using `1` (an `int`, BTW, always the same) as the first element of all the vectors. There's any chance you should use a `class` (with different members) instead of a vector of `double`s? – Bob__ Apr 12 '17 at 18:52

0 Answers0