putc 给出分段错误吗?

发布于 2024-12-07 16:22:09 字数 438 浏览 0 评论 0原文

下面的代码给出了分段错误......为什么??????使用 fputc 尝试的事件

我认为可能存在一个我无法得到的愚蠢错误..但没有足够的时间。

请帮忙...

   #include <stdio.h>

    int main () {

  //      system("sudo openssl enc -base64 -in file.txt -out file.txt.enc");

    FILE *fp,*fq;
    char ch;

    fp = fopen("file.txt.enc","r");
    fq = fopen("output.txt","w");

    while( (ch=fgetc(fp)) != EOF)
            putc(ch,fq);

    return 0;

    }

The following code is giving segmentation fault....Why ????? event tried with fputc

I think there might be a silly error i am not able to get..but don't have enogh time.

Please help...

   #include <stdio.h>

    int main () {

  //      system("sudo openssl enc -base64 -in file.txt -out file.txt.enc");

    FILE *fp,*fq;
    char ch;

    fp = fopen("file.txt.enc","r");
    fq = fopen("output.txt","w");

    while( (ch=fgetc(fp)) != EOF)
            putc(ch,fq);

    return 0;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

甜心 2024-12-14 16:22:09
  1. 您必须将 ch 声明为 int。否则,当出现字符ÿ时,文件的处理将停止。 char 只能采用 256 个不同的值,这对于 256 个不同的符号 + EOF 字符来说是不够的。 EOF-1,当视为 int 时相当于 4,294,967,295,但它相当于 当被视为 char 时,>255。如果您的输入文件包含字符ÿ(当被视为签名时,本质上是-1255),则该语句ch == EOF 将变为 true,并且您的 while 循环将中断。这与您的错误无关,但它仍然很重要...

  2. 如果您的程序崩溃,它会尝试读取/写入NULL指针,因为无法读取输入文件(不存在)或无法写入输出文件(写保护)。

尝试:

#include <stdio.h>

int main () {

    FILE *fp,*fq;
    int ch;

    if( (fp = fopen("file.txt.enc","r")) == NULL)
        return 1;

    if( (fq = fopen("output.txt","w")) == NULL)
        return 1;

    while( (ch=fgetc(fp)) != EOF)
        putc((char) ch, fq);

    return 0;

}
  1. You have to declare ch as int. Otherwise, the processing of the file will stop when the character ÿ appears. char can take only 256 different values, which is not enough for 256 different symbols + the EOF character. EOF is -1, which is equivalent to 4,294,967,295 when treated as an int, but it's equivalent to 255 when treated as a char. If your input file contains the character ÿ (essentially -1 or 255 when treated as signed), the statement ch == EOF will become true and your while loop will break. This has nothing to do with your error, but it's important nonetheless...

  2. If your program crashes, it tries to read from / write to the NULL pointer because the input file couldn't be read (doesn't exist) or the ouput file couldn't be written to (write protected).

Try:

#include <stdio.h>

int main () {

    FILE *fp,*fq;
    int ch;

    if( (fp = fopen("file.txt.enc","r")) == NULL)
        return 1;

    if( (fq = fopen("output.txt","w")) == NULL)
        return 1;

    while( (ch=fgetc(fp)) != EOF)
        putc((char) ch, fq);

    return 0;

}
稍尽春風 2024-12-14 16:22:09

可能您的其中一个 fopen 调用失败了。你没有费心去检查他们是否成功。

fopen失败时,返回空指针。如果您随后尝试使用它,那么您的程序可能会崩溃。

然后,您必须修复 Blagovest 描述的错误,当然,您应该关闭您的文件。

Probably one of your fopen calls failed. You didn't bother to check whether or not they succeeded.

When fopen fails a null pointer is returned. If you try to use that subsequently then your program will likely bomb.

You will then have to fix the bug that Blagovest describes, and you should, of course, close your files.

霞映澄塘 2024-12-14 16:22:09

fgetc 返回 int,而不是 char。其目的是能够返回 EOF 并将其与读取的字符区分开来。

只需将 ch 声明为 int 即可。

fgetc returns int, not char. The purpose of that is to be able to return EOF and distinguish that from a character that's read.

Just declare ch as an int.

悲欢浪云 2024-12-14 16:22:09

看看这个并阅读我的答案 http://www.cplusplus.com/reference/clibrary /cstdio/fgetc/

     Fgetc() \\ returns a signed int value

并且您已将 ch 声明为 char 并将其声明为 int 并尝试一下。会起作用的
也许您必须检查 fopen 真正返回给您的内容,可能是因为 fopen failed 。它在我的海湾合作委员会上工作正常

Check this out and read my answer http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/

     Fgetc() \\ returns a signed int value

and you have declared ch as char make it as int and try it out. it will work
Probably you have to check out what really the fopen is returning to you, may be its because fopen failed . its working fine on my gcc

〃温暖了心ぐ 2024-12-14 16:22:09

ch 必须是整数。请参阅 man fgetcman putc

ch must be integer. See man fgetc and man putc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文