0

Run-Time Check Failure #2 - Stack around the variable 'filename' was corrupted.

My code works whenever I try to process the first memory location. I can process the .txt file correctly, and I can print it. Nevertheless, when I ask for a second memory location, the program crashes. I tried to increase the size of filename, and I am also closing the first file, so I am clueless. Any help is acceptable! Thank you!!

This is a photo of the output

This is my code:

#include <stdio.h>

#define SIZE 100 //100 entries (100lines on a file)
#define SENSORN 100

int main()
{
    FILE *fptr;
    char filename[1000];

    char dummy1[1];//dummy character that help me to erase what is the buffer when using gets()
    int numberOfSensors;
    int time[SENSORN][SIZE];
    float powerConsumption[SENSORN][SIZE];

    int sensor_num;
    int count1 = 0;

    printf("Please enter the number of sensors: ");
    scanf("%d", &numberOfSensors);

    //Asking for the link 
    //numberOfSensors - 1 because for example, if we want only 2 sensors we need sensor0 and sensor1 only
    for (sensor_num = 0; sensor_num <= (numberOfSensors - 1); sensor_num++)
    {
        printf("Please enter the file location for sensor %d\n", sensor_num);
        gets(dummy1);//clearing the buffer
        gets(filename);

        fptr = fopen(filename, "r");

        //if file cannot be opened, then display and error message
        if (fptr == NULL)
        {
            printf("Error opening the file! %s \n", filename);
            printf("Please restart the program \n");
            return 0; //exit the program
        }

        else
        {
            //Loop that let us read and print all the file by storing each value to its respective array

            while (!feof(fptr))
            {
                //storing all the values in their respective array
                //sensor_num is the sensor number
                //count1 is the line number we are reading from the file
                fscanf(fptr, "%d %f", &time[sensor_num][count1], &powerConsumption[sensor_num][count1]);

                //making sure we have all the values stored
                //Note: do not comment the following line in order to check that all values are stored
                fprintf(stdout, "%d %6.2f \n", time[sensor_num][count1], powerConsumption[sensor_num][count1]);
                count1++;
            }
        }
        //closing file
        fclose(fptr);
        }
    }
}
artm
  • 17,291
  • 6
  • 38
  • 54

1 Answers1

1

As Martin James said char dummy1[1]; is an absolute no-no.

Use fgets() instead of gets(), so instead of

gets(dummy1);//clearing the buffer
gets(filename);`

try,

fgets( filename, sizeof(filename) , stdin );

artm
  • 17,291
  • 6
  • 38
  • 54
  • I tried that, but the program does not stop to ask me for a file location. It jumps straight to: Error opening the file! I used the gets(dummy1) in order to clear the buffer :( – Miguel Tovar Nov 28 '15 at 08:19
  • OK, now use your debugger to break after the gets(filename)' and then inspect 'filename'. – Martin James Nov 28 '15 at 08:25
  • ..and find out that the line terminator char is in the 'filename' buffer. Replace it with a NULL. – Martin James Nov 28 '15 at 08:29
  • my debugger says that the value for filename is: 0x0052fa80 "\n", and I think it is because when I entered my number of sensors, I click enter '\n', so the buffer has a '\n'. Any idea how to overcome this? D: I was reading that fgets is way better to use it on file processing, but I cannot use it because it keeps storing a \n – Miguel Tovar Nov 28 '15 at 08:38
  • I haven't tried it, but it could be an additional `\n` appended at the end of `filename` after `fgets()`. Suggest you try this line just after the `fgets()` line and see if it works: `filename[strlen(filename) - 1] = '\0';` // this remove the `\n` from `filename` – artm Nov 28 '15 at 08:52