Hello,

I have a little program that needs to get data from two separate .csv files for afterwards parse them and translate info to vectors. Everything works fine when I am working with just one file (so just one ifstream), but when I try to use another ifstream to read the other file, the program crashes and a Windows error appears showing something like (sorry for the bad translation into English):

The instruction in 0x00435508 refers to memory in 0x00323430. Memory can't be "read".

Here is the part of the code concerned, may be it is not "well coded" (containing bad coding habits) because I have just learned how to use C or CPP, but it is enough if it works.

I would appreciate any help, thank you in advance.

[code=CPP]
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdlib.h>
#include <sstream>
#include <stdexcept>
#define inputfile "cablein_.csv"
#define inputbb "BBout_.csv"
#define outfilecable "cableout_.csv"
#define sqradio 0.02

using namespace std;

void csvline_populate(vector<string> &record, const string& line, char delimiter);


int main(int argc, char *argv[])
{
    cout<< "The simulation has started"<<endl;

    long int rowsBB;
    long int rowsCables;
    double numerolargo;
    char espera;


    vector<string> row;
    vector<string> rowb;
    string line;
    string lineb;

    vector<double> xCoordCable;
    vector<double> yCoordCable;
    vector<string> ofCable;
    vector<double> xCoordBB;
    vector<double> yCoordBB;
    vector<string> ofBB;
    double distancia;


//Be sure vectors are empty
    xCoordCable.clear();
    yCoordCable.clear();
    xCoordBB.clear();
    yCoordBB.clear();
    ofBB.clear();
    ofCable.clear();


    long i = -1;

//cambiar la precision para ver todas las cifras
    cout.fixed;
    cout.precision(14);

//Import data from csv to a matrix
//----------------------------------------------------------------------

  //  ifstream in;
    ifstream in(inputfile);
    if (in.fail())
    {
        cout << "File not found" <<endl;
        return 0;
    }

    while (getline(in, line)  && in.good() )
    {
        i++;
        csvline_populate(row, line, ';');
        for (long j=0, leng=row.size(); j<leng; j++)
        {
            stringstream st;
            st.str(row[j]);
            st >> numerolargo;
//            matriznum[i][j] = numerolargo ;
           switch (j)
           {
            case 0: ofCable.push_back(row[j]); break;
            case 1: xCoordCable.push_back(numerolargo); break;
            case 2: yCoordCable.push_back(numerolargo); break;
            }
        }
    }

    in.close();

/////////Input de datos de las barras ya creadas///////////
   i=-1;
 //   ifstream inbb;
    ifstream inbb(inputbb);

    if (inbb.fail())
    {
        cout << "File not found" <<endl;
        return 0;
    }

    while (getline(inbb, lineb)  && inbb.good() )
    {
        i++;
        csvline_populate(rowb, lineb, ';');
        for (long jb=0, leng=rowb.size(); jb<leng; jb++)
        {
            stringstream st;
            st.str(rowb[jb]);
            st >> numerolargo;
//            matriznum[i][j] = numerolargo ;
           switch (jb)
           {
            case 0: break;
            case 1: xCoordBB.push_back(numerolargo); break;
            case 2: yCoordBB.push_back(numerolargo); break;
            case 3: ofBB.push_back(row[jb]);break;
            }
        }
    }

    inbb.close();

Recommended Answers

All 2 Replies

Line #115, should it rather read

case 3: ofBB.push_back(rowb[jb]);break;

Problem solved, thank you anyway.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.