fclose() 以 "Segmentation fault (core dumped)" 结尾

fclose() ends with "Segmentation fault (core dumped)"

网上没找到答案如果有请link...

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

声明字符数组至少要像

char arr[5];

并使用

fscanf(fp, "%4s", arr);

也就是需要在数组中预留一个space作为读取字符串的终止零字符'[=12=]'