运行 - 在C文件处理时检查失败#2 [英] Run - Time Check Failure #2 in C File processing
问题描述
运行时检查失败#2 - 围绕堆栈变量文件名已损坏
我的code ++工程,每当我试图处理第一个内存位置。
我能正确处理.txt文件,我可以打印出来。
然而,当我问的第二个存储位置,程序崩溃。
我试图增加文件名的大小,我也是关闭的第一个文件,让我无言以对。任何帮助是可以接受的!谢谢!!
这是我的code:
的#include<&stdio.h中GT;的#define SIZE 100 // 100个条目(在文件100lines)
#定义感应器N 100诠释的main()
{
FILE * FPTR;
字符文件名[1000]; CHAR dummy1 [1]; //无效字符,帮助我抹去的是使用时得到缓冲()
INT numberOfSensors;
INT时间[感应器N] [SIZE]
浮powerConsumption [感应器N] [SIZE] INT sensor_num;
INT的count1 = 0; 的printf(请输入传感器的数目:);
scanf函数(%d个,&安培; numberOfSensors); //问计于链接
// numberOfSensors - 1,因为,例如,如果我们只想要2个传感器,我们需要sensor0只有SENSOR1
为(sensor_num = 0; sensor_num&下; =(numberOfSensors - 1); sensor_num ++)
{
的printf(请输入传感器%d个\\ n文件位置,sensor_num);
得到(dummy1); //清除缓冲
得到(文件名); FPTR = FOPEN(文件名,R); //如果文件无法打开,然后显示与错误信息
如果(FPTR == NULL)
{
的printf(错误打开文件%s \\ n!,文件名);
的printf(请重新启动程序\\ n);
返回0; //退出程序
} 其他
{
//循环,让我们阅读和每个值存储到其各自的阵列打印所有文件 而(!的feof(FPTR))
{
//存储在各自的阵列的所有值
// sensor_num是传感器数
// COUNT1是我们从文件中读取的行号
的fscanf(FPTR,%D%F,&安培;时间[sensor_num] [COUNT1],和放大器; powerConsumption [sensor_num] [COUNT1]); //确保我们所储存的所有值
//注意:不要注释以下行,以检查是否所有的值存储
fprintf中(标准输出,%D%6.2f \\ n,时间[sensor_num] [COUNT1],powerConsumption [sensor_num] [COUNT1]);
COUNT1 ++;
}
}
//关闭文件
FCLOSE(FPTR);
}
}
}
由于马丁詹姆斯说字符dummy1 [1];
是绝对不行的。
使用与fgets()
而不是获得()
,所以不是
获取(dummy1); //清除缓冲区
得到(文件名);`
尝试,
与fgets(文件名的sizeof(文件名),标准输入);
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 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);
}
}
}
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 );
这篇关于运行 - 在C文件处理时检查失败#2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!