与龟etc使用整数变量() [英] integer variable used with fgetc()

查看:124
本文介绍了与龟etc使用整数变量()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想了解一些基本的code和按照code

稍微得到了困惑

  INT的main()
{
   FILE * FP;
   INT℃;
   INT N = 0;   FP = FOPEN(file.txt的,R);
   如果(FP == NULL)
   {
      PERROR(错误打开文件);
      返回(-1);
   }
   做
   {
      C =龟etc(FP);
      如果(的feof(FP))
      {
         突破;
      }
      的printf(%C,C);
   }而(1);   FCLOSE(FP);
   返回(0);
}

有人能解释我为什么c是整数类型,即使它是由定义龟etc(FP)其中,从我的知识,只是取一个字符<? / p>

解决方案

考虑到这种特殊的code已经被写入了precise方式, C 止跌吨需要是类型 INT - 它仍然会如果工作ç是类型

大多数code,从某个文件(至少在初期)读取字符读取这些字符到 INT 虽然。具体而言,C风格我的基本设计的一部分/ O是,像函数 GETC 龟etc 可以返回EOF除了任何值可能来自该文件。这就是说,类型的任何值字符可从文件中读取。 GETC 龟etc 等,可以返回至少一个值的韩元信号文件的末尾'T 的/的无法的有来自该文件。它通常是-1,但是这不能保证。当字符签署(通常是这样,现在),你可以从文件中获取的值将显示为-1时,它被分配到一个字符,所以如果你根据返回值来检测EOF,这可能会导致错误检测。

您已经包含在问题上的code简单地将文件复制到标准输出的内容,一次一个字符。利用EOF的返回值,我们可以简化code一点点,所以主循环是这样的:

  INT℃;而(EOF!=(C =龟etc(FP)))
    的printf(%C,C); //或:的putchar(C);

我想有些人可能会觉得这过于简洁。其他人可能会反对环路的条件内的分配。我,至少,认为这些都是合理的和有效参数,但仍足以适合更好地与它的preferable库的设计。

I was trying to understand some basic code and got slightly confused by following code

int main ()
{
   FILE *fp;
   int c;
   int n = 0;

   fp = fopen("file.txt","r");
   if (fp == NULL)
   {
      perror("Error in opening file");
      return(-1);
   }
   do
   {
      c = fgetc(fp);
      if ( feof(fp) )
      {
         break ;
      }
      printf("%c", c);
   } while(1);

   fclose(fp);
   return(0);
}

Can someone explain me why c is of type integer, even though it is defined by fgetc(fp) which, from my knowledge, gets just the next character?

解决方案

Given the precise way this particular code has been written, c wouldn't need to be of type int--it would still work if c were of type char.

Most code that reads characters from a file should (at least initially) read those characters into an int though. Specifically, part of the basic design of C-style I/O is that functions like getc and fgetc can return EOF in addition to any value that could have come from the file. That is to say, any value of type char could be read from the file. getc, fgetc, etc., can signal the end of file by returning at least one value that won't/can't have come from the file. It's usually -1, but that's not guaranteed. When char is signed (which it usually is, nowadays) you can get a value from the file that will show up as -1 when it's been assigned to a char, so if you're depending on the return value to detect EOF, this can lead to mis-detection.

The code you've included in the question simply copies the contents of the file to standard output, one character at a time. Making use of the EOF return value, we can simplify the code a little bit, so the main loop looks like this:

int c;

while (EOF != (c = fgetc(fp)))
    printf("%c", c); // or: putchar(c);

I suppose some might find this excessively terse. Others may object to the assignment inside the condition of the loop. I, at least, think those are reasonable and valid arguments, but this still fits enough better with the design of the library that it's preferable.

这篇关于与龟etc使用整数变量()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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