求助 linux下feof的问题

buaa_dep6 2008-06-12 10:20:40
这是我一个加密程序的代码。程序的功能是把in.txt中的内容进行加密然后输出到out.txt中。调用一个我写的函数加密
以下是我的代码(加密函数部分略)。
char encrypt(char ch,int n);
int main(){
int n;
FILE*fin, *fout;
scanf("%d", &n);
if((fin = fopen("in.txt", "r")) == NULL)
exit(0);
if((fout = fopen("out.txt", "w")) == NULL)
exit(1);
while(!feof(fin)){
fputc(encrypt(fgetc(fin),n),fout);
}
fclose(fin);
fclose(fout);
return 0;
}

我在vc++6.0和dev— c++中跑都没有问题,但提交到我们学校的评测系统上就一个测试点也过不了,他用的是linux系统。
后来我将while(!feof(fin))这句改为while((c = fgetc(fin)) != EOF)再提交就通过了所有测试点,后来查了一下说是feof是当作宏来用的,但linux中feof却实现为函数。
那么请问linux下feof应该如何使用,我那么写为什么就出问题呢?
ps:由于测试平台不同我写好感觉没问题的程序有时提交上去会一个测试点也不能通过甚至编译不能通过死循环等等。那么请问linux下c编程和windows下到底还有哪些差别呢?
初学编程,见笑于大方之家了。谢谢
...全文
132 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
buaa_dep6 2008-06-17
  • 打赏
  • 举报
回复
新注册的,没有多少分。20分平分吧。
分少望见谅。
buaa_dep6 2008-06-17
  • 打赏
  • 举报
回复
原来是这样。
谢谢啦:)
mymtom 2008-06-16
  • 打赏
  • 举报
回复
原因在于feof返回零并不能保证fgetc返回成功。
感觉上好像WINDOWS下,提前设置了eof标志,
就是说,如果你上次fgetc读到的是最后一个字节,EOF标志就被设置了。

而且用feof判断是有问题的,fgetc返回EOF,可能的原因是读错误或者EOF,要用feof和ferror来进行区分。

[code=BatchFile]
1 #include <stdio.h>
int fgetc(FILE *stream);
Description
2 If a next character is present from the input stream pointed to by stream, the fgetc
function obtains that character as an unsigned char converted to an int and
advances the associated file position indicator for the stream (if defined).
Returns
3 The fgetc function returns the next character from the input stream pointed to by
stream. If the stream is at end-of-file, the end-of-file indicator for the stream is set and
fgetc returns EOF. If a read error occurs, the error indicator for the stream is set and
fgetc returns EOF.230)
[/code]
cceczjxy 2008-06-16
  • 打赏
  • 举报
回复
feof判断的一般会多读一遍。
使用feof时你要判断fgetc是否成功。
buaa_dep6 2008-06-13
  • 打赏
  • 举报
回复
没有人知道么?

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧