小编thr*_*six的帖子

fprintf分段错误 - 解释就像我5

我正在尝试将一些字符串写入文件.编译时没有任何警告,但是当我运行a.out时会出现段错误.但它会创建目标文件.我是C的新手,所以我为我的格式和其他缺点道歉.这是我的代码:

#include<stdio.h>

int main (void)
{
    FILE *fp_out;

    char str1[]="four score and seven years ago our";
    char str2[]="fathers broughy forth on this continent,";
    char str3[]="a new nation, concieved in Liberty and dedicated";
    char str4[]="to the proposition that all men are created equal.";

    fp_out=fopen("my_file", "w");

    if(fp_out!=NULL)
    {
        fprintf(fp_out,"%s\n", str1[100]);
        fprintf(fp_out,"%s\n", str2[100]);
        fprintf(fp_out,"%s\n", str3[100]);
        fprintf(fp_out,"%s\n", str4[100]);
        fclose(fp_out);
    }
    else
        printf("The file \"my_file\" couldn't be opened\n");

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c printf segmentation-fault

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c ×1

printf ×1

segmentation-fault ×1