【问题标题】:reading an integer from a file从文件中读取整数
【发布时间】:2011-04-26 07:35:20
【问题描述】:

我有一个假设从文件中读取整数的代码。但它实际上是作为一个角色来阅读的。建议我进行一些修改,以便我可以将整数读入数组。

fptr =fopen("path","r");

while(1)
{
  c=getc(fptr);
  putchar(c);
  if (c==EOF)
    exit(1);
}

提前致谢

阿米特

【问题讨论】:

    标签: c arrays file integer strtol


    【解决方案1】:
    #include <stdio.h>
    int main(int argc, char **argv ) {
        int value;
        FILE *fp = fopen ( "d:\\abc.txt", "r");
        while ( fscanf(fp, "%d", &value) == 1 ) {       
            printf ( "%d\n", value );
        }
        fclose ( fp );
    }
    

    【讨论】:

      【解决方案2】:

      你可以像这样使用fscanf

      int a;
      
      while (fscanf(fptr, "%d", &a) == 1)
      {
          printf("Read %d\n", a);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-06-22
        • 1970-01-01
        • 2018-08-23
        • 2013-07-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        相关资源
        最近更新 更多