0

I'm getting this exception while trying to read from text file in c++.

Windows has triggered a breakpoint in myprogram.exe.

This may be due to a corruption of the heap, which indicates a bug in myprogram.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while myprogram.exe has focus.

The output window may have more diagnostic information.

The text file contains numbers that i want to save in a double array, this is my code:

double* breakLine(char arr[])
{
    double* row_data = new double[FILE_ROW_PARAMS];
    int j= 0, m= 0,k= 0;
    char str[FILE_ROW_SIZE]; 
    while(arr[j] != '\0')//not end of line
    {
        if(arr[j] == ' ')//significant data
        {
            str[k] = '\0';
            row_data[m++] = atoi(str);
            k = 0;
        }
        else if(arr[j]!=' ')
        {
            str[k++] = arr[j];
            if(arr[j+1] == '\0')
            {
                str[k] = '\0';
                row_data[m] = atoi(str);
            }
        }
        j++;
    }
    return row_data;
}

double* readFile(char fileName[],int* num,double* params)
{
    int size= SIZE, number_of_lines= 0;
    double* points = new double[SIZE * FILE_ROW_PARAMS];
    char arr[128];
    ifstream infile;
    infile.open(fileName);
    if(!infile) 
    {
        cout<<"NO FILE!";
        return NULL;
    }
    else
    {
        for(int i=0; i < NUMPARAM; i++) //get first params
        {
            infile >> params[i];
            size--;
        }
        infile.getline(arr,128);
        while(!infile.eof())
        {
            infile.getline(arr,128);
            double* a = breakLine(arr);
            for(int i=0; i < FILE_ROW_PARAMS; i++)
            {
                *(points+number_of_lines*FILE_ROW_PARAMS+i) = *(a+i);
            }
            number_of_lines++;
            size--;
            if(size == 0)
            {
                size = SIZE;
                points = (double*)realloc(points, number_of_lines + SIZE * FILE_ROW_PARAMS);
            }
        }
        infile.close();
        *num  = number_of_lines;
        return points;
    }
}
cHao
  • 78,897
  • 19
  • 136
  • 168
Tamar Cohen
  • 1,182
  • 2
  • 14
  • 28
  • 'while(arr[j] != '\0')//not end of line' -- Are you searching for the end of line (\n) or for the end of the string (\0)? – pearcoding Nov 03 '13 at 11:27
  • 1
    related: [don't do this: `while(!infile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Nov 03 '13 at 11:29
  • end of line in the file = end of string... one line is one string – Tamar Cohen Nov 03 '13 at 11:30
  • i've changed !infile.eof() to !(infile.getline(arr,128)).eof() and still getting the same error – Tamar Cohen Nov 03 '13 at 11:40
  • 1
    You really should be using [`std::vector`](http://en.cppreference.com/w/cpp/container/vector) and [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string). – 0x499602D2 Nov 03 '13 at 12:36

1 Answers1

0

I'm not completely sure about the structure of your text file. But you're not taking advantage of the C++ library. Here's a code example that reads the numbers from a text file, line by line and pushes them into a vector of doubles. It also counts the lines as you are. I note that you were reading integers (you are converting the input using atoi()), so my example does too. In this example I am ignoring the initial parameters that you read from your file.

#include <fstream>
#include <string>
#include <vector>
#include <sstream>

int main()
{
    std::ifstream infile("c:\\temp\\numbers.txt");
    std::vector<double> result;
    std::string line;
    int lines = 0;
    while (std::getline(infile, line))
    {
        ++lines;
        std::stringstream instream(line);
        std::copy(std::istream_iterator<int>(instream), std::istream_iterator<int>(), std::back_inserter(result));
    }

    return 0;
}
Peter R
  • 2,517
  • 14
  • 18