输出没有在文件操作正确displying [英] Output is not displying correctly in file operation

查看:101
本文介绍了输出没有在文件操作正确displying的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释什么是这个code中的错误。

Can anybody explain what is the mistake in this code.

#include<stdio.h>
int main() {
  FILE *f1;
  char c;
  f1 = fopen("INPUT", "w");
  while((c=getchar()) != '/')
    putc(c, f1);
  fclose(f1);
  f1 = fopen ("INPUT", "r");
  while ((c = getc(f1) != EOF))
    printf("%c", c); 
  fclose(f1);
}

的输出在检测不到字体来。我在Windows尝试也。但同样的结果。

The output is coming in undetectable font. I tried in windows also. But the same result.

推荐答案

首先, C 应该是 INT ,而不是一个字符。 putc将()需要一个 INT ,更重要的是,GETC()从流中读取下一个字符,并将其作为一个 unsigned char型强制转换为 INT ,或在EOF文件或错误结束。如果你把它存储到一个字符相反, EOF ,因为字符太窄,无法重新present这一点。 GETC()

First, c should be an int, not a char. putc() takes an int and, more importantly, getc() reads the next character from the stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. If you store it into a char instead, EOF gets lost since char is too narrow and can't represent that. getc()

第二,这是错误的:

while ((c = getc(f1) != EOF))

你想要的这里是:

what you want here is:

while ((c = getc(f1)) != EOF)

您已经错位括号。

记住,你需要改变,因为您的printf()呼叫c 现在是一个 INT

Remember that you need to alter your printf() call since c is now an int:

printf("%c", (char)c);

您需要显式的造型,因为printf()函数是一个可变参数函数,因此编译器执行不会自动类型转换。你需要用可变参数的函数手动施放。

You need the explicit cast because printf() is a variadic function and therefore the compiler performs no automatic type conversion. You need to cast manually with variadic functions.

这篇关于输出没有在文件操作正确displying的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆