1

I have double numbers in a file (one on each line) that I am trying to read into a c++ array. I am using the below code, but get the below error while running:

segmentation fault: 11

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

int main () {
    string line;
    ifstream myfile ("temp2.csv");
    std::vector<double> myArray;
    int index = 0;
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            cout << line << endl;
//            myArray[index++] << line;
            myArray[index++] = atoi( line.c_str() );
        }
        myfile.close();
    }

    else cout << "Unable to open file";

    return 0;
}
13
  • 2
    Please note that while (!myfile.eof()) is considered bad practice. Commented Sep 22, 2016 at 15:17
  • 1
    What does your file look like Commented Sep 22, 2016 at 15:17
  • Why can I not find a dupe for this?!? The problem is your vector is empty. Commented Sep 22, 2016 at 15:19
  • @Rakete1111 - it's not "considered bad practice"; it's simply wrong. <g> Commented Sep 22, 2016 at 15:26
  • @PeteBecker Oops, yeah you're right :) Thanks for mentioning Commented Sep 22, 2016 at 15:27

3 Answers 3

2

You can't do

myArray[index++] = atoi( line.c_str() );

It is an empty vector. You either need to push_back elements into it. Or initialize it with sufficient memory.

This should work:

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

int main () {
    string line;
    ifstream myfile ("temp2.csv");
    std::vector<double> myArray;
    int index = 0;
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            cout << line << endl;
//            myArray[index++] << line;
            myArray.push_back(atoi( line.c_str() ));
        }
        myfile.close();
    }

    else cout << "Unable to open file";

    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

On this line:

std::vector<double> myArray;

You create a vector using the default constructor. As you can see from the documentation, the default constructor creates an empty vector.

On this line:

myArray[index++] = atoi( line.c_str() );

You access elements from the vector with successively increasing indices. But those elements do not exist and the indices are out of bounds because the vector is empty. Accessing outside the bounds of the vector has undefined behaviour.

TL;DR you forgot to add any elements into the vector.

Comments

1

The code is far more complex than it needs to be. It's much simpler to just read one value at a time:

std::vector<double> myArray;
double value;
std::ifstream myfile(temp2.csv);
if (!myfile) {
    std::cout << "Unable to open file\n");
    return EXIT_FAILURE;
}
while (myfile >> value)
    myArray.push_back(value);

2 Comments

It seems that OP wants to read only one value per line, and ignore the rest. This code reads all values, and also expects that they are separated by spaces/tabs (while the file format is CSV).
@anatolyg - the question not say "read only one value per line and ignore the rest". That's a guess you're making from reading the non-working code. Further, note that the question does not say "file format is CSV". Again, that's a guess you've made based on the file extension. Not an unreasonable guess, but coding decisions should not be based on guesses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.