putc 给出分段错误吗?
下面的代码给出了分段错误......为什么??????使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须将
ch
声明为int
。否则,当出现字符ÿ时,文件的处理将停止。char
只能采用 256 个不同的值,这对于 256 个不同的符号 + EOF 字符来说是不够的。EOF
为-1
,当视为int
时相当于4,294,967,295
,但它相当于当被视为
。如果您的输入文件包含字符ÿ(当被视为char
时,>255签名
时,本质上是-1
或255
),则该语句ch == EOF
将变为 true,并且您的while
循环将中断。这与您的错误无关,但它仍然很重要...如果您的程序崩溃,它会尝试读取/写入
NULL
指针,因为无法读取输入文件(不存在)或无法写入输出文件(写保护)。尝试:
You have to declare
ch
asint
. 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 + theEOF
character.EOF
is-1
, which is equivalent to4,294,967,295
when treated as anint
, but it's equivalent to255
when treated as achar
. If your input file contains the character ÿ (essentially-1
or255
when treated assigned
), the statementch == EOF
will become true and yourwhile
loop will break. This has nothing to do with your error, but it's important nonetheless...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:
可能您的其中一个
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.
fgetc
返回int
,而不是char
。其目的是能够返回 EOF 并将其与读取的字符区分开来。只需将
ch
声明为int
即可。fgetc
returnsint
, notchar
. The purpose of that is to be able to returnEOF
and distinguish that from a character that's read.Just declare
ch
as anint
.看看这个并阅读我的答案 http://www.cplusplus.com/reference/clibrary /cstdio/fgetc/
并且您已将 ch 声明为 char 并将其声明为 int 并尝试一下。会起作用的
也许您必须检查 fopen 真正返回给您的内容,可能是因为 fopen failed 。它在我的海湾合作委员会上工作正常
Check this out and read my answer http://www.cplusplus.com/reference/clibrary/cstdio/fgetc/
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
ch 必须是整数。请参阅
man fgetc
和man putc
。ch must be integer. See
man fgetc
andman putc
.