Handle Exception Error

Jan 24, 2017 at 1:14pm
Good afternoon,

I am working on a program that read a csv file and then generate a csv file as an output.
I got the following error:
Exception thrown at 0x00C02EEF in REdEdgeApp.exe: 0xC0000005: Access violation writing location 0x8F9B1FDC.
This is the code of the main program:
// MicaRedEdge.cpp : Defines the entry point for the console application
#pragma warning disable
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

#include "csvparser.h"

#include "string.h"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4996)
#pragma warning(disable:4083)
#endif
/********************************************************************************
*
* Define Directives
*
********************************************************************************/
#define max(A,B) ((A) > (B) ? (A) : (B))

/********************************************************************************
*
* Global Variables
*
********************************************************************************/
const double DBL_MISSING_VALUE = 0/* value needed here! */;
const int INT_MISSING_VALUE = 0/* value needed here! */;

/********************************************************************************
*
* External Variable and Function Declarations
*
********************************************************************************/
double MARS(double BLUE, double RED, double REDEDGE, double NIR);

int main() {

int i = 0;
double N[100], BLUE[100], RED[100], REDEDGE[100], NIR[100], Y[100];
//Read the input file in cvs format (file, delimiter, first_line_is_header)
CsvParser *csvparser = CsvParser_new("input.csv", ",", 1);
CsvRow *header;
CsvRow *row;
header = CsvParser_getHeader(csvparser);
if (header == NULL) {
printf("%s\n", CsvParser_getErrorMessage(csvparser));
return 1;
}
const char **headerFields = CsvParser_getFields(header);
for (i = 0; i < CsvParser_getNumFields(header); i++) {
printf("TITLE: %s\n", headerFields[i]);
}
// Do NOT destroy the headear manually if you plan to destroy the parser later.
// If you destroy both header and parser, you will get double free runtime error
// CsvParser_destroy_row(header);
i = 0;
while ((row = CsvParser_getRow(csvparser))) {
printf("==NEW LINE==\n");
const char **rowFields = CsvParser_getFields(row);
N[i] = atof(rowFields[0]);
BLUE[i] = atof(rowFields[1]); //THIS IS THE LINE THAT GENERATE ERROR//
RED[i] = atof(rowFields[2]);
REDEDGE[i] = atof(rowFields[3]);
NIR[i] = atof(rowFields[4]);
printf("NITROGEN VALUE: %.2f\n", N[i]);
printf("BLUE VALUE: %.2f\n", BLUE[i]);
printf("RED VALUE: %.2f\n", RED[i]);
printf("REDEDGE VALUE: %.2f\n", REDEDGE[i]);
printf("NIR VALUE: %.2f\n", NIR[i]);
Y[i] = MARS(BLUE[i], RED[i], REDEDGE[i], NIR[i]);
printf("Y is: %.1f\n", Y[i]);
i++;

printf("\n");
CsvParser_destroy_row(row);
}
CsvParser_destroy(csvparser);
//Generate the ouput file with the following columns: N, blue, red, rededge, Nir and Y
FILE *f = fopen("Output.csv", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}

fprintf(f, "N_Content,Blue,Red,RedEdge,NIR,Y\n");

int h;
for (h = 0;h<i;h++)
fprintf(f, "%.2f,%.2f,%.2f,%.2f,%.2f,%.1f\n", N[h], BLUE[h], RED[h], REDEDGE[h], NIR[h], Y[h]);
fclose(f);


getchar();
return 0;
}

The csv file has 1049 lines. However, the application worked before with a file of 40 lines.

I really appreciate if someone can help me to sort out the problem.

Thanks,

Nubia
Last edited on Jan 24, 2017 at 1:15pm
Jan 24, 2017 at 1:37pm
You should check that i < 100 before you write to your array.
1
2
3
4
5
N[i] = atof(rowFields[0]);
BLUE[i] = atof(rowFields[1]); //THIS IS THE LINE THAT GENERATE ERROR//
RED[i] = atof(rowFields[2]);
REDEDGE[i] = atof(rowFields[3]);
NIR[i] = atof(rowFields[4]);


If you don't know how many lines the file has it would be much better to use vectors instead of arrays.
Jan 24, 2017 at 1:56pm
You also need to check that the file contains all those fields:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        if (CsvParser_getNumFields(row) > 4)
        {
            N[i]       = atof(rowFields[0]);
            BLUE[i]    = atof(rowFields[1]); // THIS IS THE LINE THAT GENERATE ERROR //
            RED[i]     = atof(rowFields[2]);
            REDEDGE[i] = atof(rowFields[3]);
            NIR[i]     = atof(rowFields[4]);
            printf("NITROGEN VALUE: %.2f\n", N[i]);
            printf("BLUE VALUE: %.2f\n", BLUE[i]);
            printf("RED VALUE: %.2f\n", RED[i]);
            printf("REDEDGE VALUE: %.2f\n", REDEDGE[i]);
            printf("NIR VALUE: %.2f\n", NIR[i]);
            Y[i] = MARS(BLUE[i], RED[i], REDEDGE[i], NIR[i]);
            printf("Y is: %.1f\n", Y[i]);
            i++;
    
            printf("\n");
        }
Topic archived. No new replies allowed.