Question

I have to read from a text file that contains a bunch of binary numbers and then convert them to integers and store these in an array. I have made a function to do this, however, the function is only returning one number. It seems to not be going through the file, or it just doesn't work. Can anyone figure out why?

void readf4()
{
    std::ifstream inFile("f4.txt");
    std::vector<int> intArray;
    std::string line;
    //unsigned num = 0;
    int inc = 0;
    char * pEnd;
    for(unsigned long int result; std::getline(inFile,line); result = strtol(line.c_str(),&pEnd,10))
    {
        //seg fault if I include these lines
        //intArray[inc] = result;
        //inc++;

        cout<<result;//always returns 9223372036854775807
        cout<<"\n";
    }
}

Thanks in advance!

Was it helpful?

Solution

The problem you have is that you use an empty vector and try to assign something to its element.

You need to use

intArray.push_back(result);

And you need to initialize result first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top