编码程序分割错误

segmentation fault with encoding program

本文关键字:错误 分割 程序 编码      更新时间:2023-10-16
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc, char* argv[])
{
    int n;
    FILE *f1;
    char fn[]={"test.txt"};
    int c;
    f1=fopen(fn,"r");
    c=fgetc(f1);
    while(c!=EOF){
        if ((c<'Z')&&(c>'A')) c=c+n;
    }
    fputc(c,f1);
    fclose(f1);
}

我正试图写一个程序编码(!?我不知道是不是这么叫的,因为英语是我的第二语言)。如果我输入的步骤数为4,它应该改变A为E, B为F,等等。但一直显示"分段故障(堆芯)"。我不知道怎么了,请帮帮我。

我在你的代码中发现了这些问题:

  • n未在程序中初始化

  • r是只读模式,为了写,你必须使用w+r+

  • fputc( int c, FILE *fp );该功能可用于对文件进行写操作。它返回成功时写入的字符,如果有错误则返回EOF。

  • 分割错误是由于在文件程序中使用r模式,请尝试使用w+模式运行程序。