提问人:Francesco 提问时间:5/30/2022 最后编辑:Vlad from MoscowFrancesco 更新时间:5/30/2022 访问量:114
fclose() 以 “Segmentation fault (core dumped)” 结尾
fclose() ends with "Segmentation fault (core dumped)"
问:
我没有在互联网上找到答案,如果有的话,请链接它......
#include <stdio.h>
int main(){
FILE*fp;
char arr[4];
printf("Opening...\n");
fp=fopen("slide.txt", "r");
printf("Opened\n");
if(fp==NULL){
printf("No such file\n");
return 1;
}
fscanf(fp, "%s", arr);
printf("Printing...\n");
printf("%s\n", arr);
printf("Printed\n");
printf("Closing...\n");
fclose(fp);
printf("Closed\n");
return 0;
}
文件内容:
ciao
我有的输出:
Opening...
Opened
Printing...
ciao
Printed
Closing...
Segmentation fault (core dumped)
答:
9赞
Vlad from Moscow
5/30/2022
#1
声明字符数组至少像
char arr[5];
并使用
fscanf(fp, "%4s", arr);
也就是说,您需要在数组中为读取字符串的终止零字符保留一个空间。'\0'
评论