gpt4 book ai didi

c - 在简单程序中使用 ferror() 时出现段错误。为什么?

转载 作者:太空宇宙 更新时间:2023-11-04 00:14:45 27 4
gpt4 key购买 nike

为什么下面的代码会出现段错误?

#include <stdio.h>
#include <stdlib.h>

int main()
{
FILE *file;
file = fopen("text","r");
if (file == NULL) printf("Error READING FILE");
if (ferror(file)) printf("error reading file"); //line 9
return 0;
}

在 gdb 中进行回溯给出:-

> #0  0x00007ffff7ad9d30 in ferror () from /lib/libc.so.6> #1  0x00000000004005fa in main () at test.c:9

最佳答案

文件为空。您没有看到第一个 printf,因为程序在刷新 stdout 之前崩溃了。

关于c - 在简单程序中使用 ferror() 时出现段错误。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2036307/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com