Parser.exe 中的 (ntdll.dll) 抛出异常:读取位置访问冲突
Exception thrown at (ntdll.dll) in Parser.exe: Access violation reading location
所以,这是迄今为止困扰我的章节
void CNCread(fPointer){
printf("\n");
fPointer = fopen ("CNCG.txt", "r");
char line[30];
while(!feof(fPointer)){
fgets( line, 150, fPointer);
puts(line);
}
fclose (fPointer);
return;
}
编译后出现以下错误,运行 并执行此函数:
在 Parser.exe 中的 0x00007FFCA1DEEAC5 (ntdll.dll) 抛出异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。
我刚刚将此项目从 Code::Blocks 转换为 Visual Studio 2015,添加了 legacy_stdio_definitions.lib 等,所以这也不是问题,但代码在 code::blocks 下运行良好.
提前感谢大家。
首先,char line[30]
最多可以包含 30 char
,但您正试图通过 fgets( line, 150, fPointer);
.
向其中写入更多内容
此外,您没有检查 fopen
是成功还是失败。你还应该检查 fgets
是成功还是失败。
此外,您可以在函数内部声明 fPointer
,而不是将其作为函数参数。它应该是 FILE *
.
类型
void CNCread(/* fPointer */){
printf("\n");
FILE *fPointer = fopen ("CNCG.txt", "r");
/* Check if fopen succeded */
if (fPointer == NULL) {
fprintf(stderr, "Error: Cannot open file to read\n");
/* Some code */
return;
}
char line[30];
while(!feof(fPointer)){
/* You are writing more chars to line than its capacity */
/* fgets( line, 150, fPointer); */
/* Change it to write at max 30 chars to line */
if (fgets( line, 30, fPointer) != NULL)
puts(line);
}
if (fclose(fPointer) == EOF) {
fprintf(stderr, "Error: Cannot close the file after reading\n");
/* Some code */
return;
}
return;
}
所以,这是迄今为止困扰我的章节
void CNCread(fPointer){
printf("\n");
fPointer = fopen ("CNCG.txt", "r");
char line[30];
while(!feof(fPointer)){
fgets( line, 150, fPointer);
puts(line);
}
fclose (fPointer);
return;
}
编译后出现以下错误,运行 并执行此函数:
在 Parser.exe 中的 0x00007FFCA1DEEAC5 (ntdll.dll) 抛出异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。
我刚刚将此项目从 Code::Blocks 转换为 Visual Studio 2015,添加了 legacy_stdio_definitions.lib 等,所以这也不是问题,但代码在 code::blocks 下运行良好. 提前感谢大家。
首先,char line[30]
最多可以包含 30 char
,但您正试图通过 fgets( line, 150, fPointer);
.
此外,您没有检查 fopen
是成功还是失败。你还应该检查 fgets
是成功还是失败。
此外,您可以在函数内部声明 fPointer
,而不是将其作为函数参数。它应该是 FILE *
.
void CNCread(/* fPointer */){
printf("\n");
FILE *fPointer = fopen ("CNCG.txt", "r");
/* Check if fopen succeded */
if (fPointer == NULL) {
fprintf(stderr, "Error: Cannot open file to read\n");
/* Some code */
return;
}
char line[30];
while(!feof(fPointer)){
/* You are writing more chars to line than its capacity */
/* fgets( line, 150, fPointer); */
/* Change it to write at max 30 chars to line */
if (fgets( line, 30, fPointer) != NULL)
puts(line);
}
if (fclose(fPointer) == EOF) {
fprintf(stderr, "Error: Cannot close the file after reading\n");
/* Some code */
return;
}
return;
}