【问题标题】:Run - Time Check Failure #2 in C File processing运行 - C 文件处理中的时间检查失败 #2
【发布时间】:2015-11-28 07:40:08
【问题描述】:

运行时检查失败 #2 - 变量“文件名”周围的堆栈已损坏。

每当我尝试处理第一个内存位置时,我的代码都会起作用。 我可以正确处理 .txt 文件,并且可以打印它。 然而,当我要求第二个内存位置时,程序崩溃了。 我试图增加文件名的大小,并且我也在关闭第一个文件,所以我一无所知。任何帮助都是可以接受的!谢谢!!

This is a photo of the output

这是我的代码:

#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);
        }
    }
}

【问题讨论】:

  • 'char dummy1[1] 不,不!
  • 如果您刚刚输入 [256],默认情况下,您不会遇到 NULL 终止符越界写入的问题!

标签: c runtime runtime-error file-processing


【解决方案1】:

正如 Martin James 所说,char dummy1[1]; 是绝对不行的。

fgets()代替gets(),所以代替

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

试试,

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

【讨论】:

  • 我试过了,但程序并没有停下来询问我的文件位置。它直接跳转到:打开文件时出错!我使用了gets(dummy1)来清除缓冲区:(
  • 好的,现在使用你的调试器在gets(filename)'之后中断,然后检查'filename'。
  • ..并找出行终止符字符在“文件名”缓冲区中。将其替换为 NULL。
  • 我的调试器说文件名的值是:0x0052fa80 "\n",我想是因为当我输入我的传感器数量时,我点击输入'\n',所以缓冲区有一个'。知道如何克服这个问题吗? D:我读到 fgets 在文件处理中使用它更好,但我不能使用它,因为它一直存储一个 \n
  • 我没有尝试过,但它可能是在filename 末尾附加\nfgets() 之后。建议您在fgets() 行之后尝试此行,看看它是否有效:filename[strlen(filename) - 1] = '\0'; // 这从filename 中删除\n
猜你喜欢
  • 2013-02-26
  • 2016-08-24
  • 2017-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多