Note that there are some explanatory texts on larger screens.

plurals
  1. POerror reading from file C++
    text
    copied!<p>I'm getting this exception while trying to read from text file in c++.</p> <p>Windows has triggered a breakpoint in myprogram.exe.</p> <p>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.</p> <p>This may also be due to the user pressing F12 while myprogram.exe has focus.</p> <p>The output window may have more diagnostic information.</p> <p>The text file contains numbers that i want to save in a double array, this is my code:</p> <pre><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&lt;&lt;"NO FILE!"; return NULL; } else { for(int i=0; i &lt; NUMPARAM; i++) //get first params { infile &gt;&gt; params[i]; size--; } infile.getline(arr,128); while(!infile.eof()) { infile.getline(arr,128); double* a = breakLine(arr); for(int i=0; i &lt; 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload