简体   繁体   English

在C中逐行读取整数

[英]Read integer line by line in c

I know it's asked before, but I haven't found for what I was searching for. 我知道之前已经有人问过,但是我没有找到我要寻找的东西。

I have a text document that have: 我有一个文本文件,具有:

1 2 3 .
2 3 4 5
3 1 2 .

and i have 4 arrays that I need to put each number on the line in one of them 我有4个数组,我需要将每个数字放在其中一个的行上
here's my code 这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
  FILE *file = fopen("d:\\rf.txt", "r");
  int ch[3];
  int a[3],b[3],c[3];
  int z,x,t,v,i;
  if (file  == NULL) return 1;
  for(i=1;i<=3;i++)
  {
      fscanf(file,"%d %d %d %d",&z,&x,&t,&v);
      ch[i]=z;
      a[i]=x;b[i]=t;c[i]=v;
      printf("%d %d %d %d\n",ch[i],a[i],b[i],c[i]);
  }

  return 0;
}

that's what I get: 那就是我得到的:

1 2 3 0
1 2 3 0
1 2 3 0

Thanks 谢谢

Your code reaches the point where there is a dot '.' 您的代码到达了点'.' symbol in the input file, and stops. 符号在输入文件中,然后停止。 Since it cannot read it with the %d format specifier, it does not advance the read pointer, so the rest of the input is essentially ignored. 由于无法使用%d格式说明符读取它,因此它不会推进读取指针,因此,其余输入基本上被忽略。

Here is one way of fixing this: 这是解决此问题的一种方法:

// int z,x,t,v,i; <<== Comment this out
if (file  == NULL) return 1;
for(i=0;i<3;i++) { // <<= Note that indexes go from 0 to 2, not from 1 to 3
    int z;
    if (fscanf(file,"%d",&z) == 1) {
        ch[i]=z;
    } else {
        ch[i]=0;
        fscanf(file,"%*s"); // Ignore the input
    }
    if (fscanf(file,"%d",&z) == 1) {
        a[i]=z;
    } else {
        a[i]=0;
        fscanf(file,"%*s"); // Ignore the input
    }
    if (fscanf(file,"%d",&z) == 1) {
        b[i]=z;
    } else {
        b[i]=0;
        fscanf(file,"%*s"); // Ignore the input
    }
    if (fscanf(file,"%d",&z) == 1) {
        c[i]=z;
    } else {
        c[i]=0;
        fscanf(file,"%*s"); // Ignore the input
    }

    printf("%d %d %d %d\n",ch[i],a[i],b[i],c[i]);
}

Note the use of asterisks with %s format on the lines that read the data to be ignored. 请注意,在读取要忽略的数据的行上使用了%s格式的星号。 The asterisk * tells fscanf to drop the result of the read. 星号*告诉fscanf删除读取结果。

Demo on ideone. ideone上的演示。

First - your for loop is not true, I don't think you meant it to be this way. 首先-您的for循环不正确,我认为您不是故意这样的。 It's supposed to be for(i = 0 ; i < 3 ; i++) as arrays initiallized by a constant SIZE are starting from 0 and going on to SIZE-1 so an array int a[3] would have a[0], a[1], a[2] and not a[1], a[2], a[3] . 假定为for(i = 0 ; i < 3 ; i++)因为由常量SIZE初始化的数组从0开始并继续到SIZE-1,所以数组int a[3] a[0], a[1], a[2]而不是a[1], a[2], a[3] About the rest of the code... 关于其余的代码...

1 2 3 .
2 3 4 5
3 1 2 .

is the file so what do . 是文件,怎么办. supposed to mean ? 应该是什么意思? It's not an integer, it'll be converted to its ASCII value (look it up on google for more information), which is basically a value from 0 to 255 that represents a character - so it actually won't be an error reading, but I don't think that's the result you meant. 它不是整数,它将被转换为ASCII值(在Google上查找以获取更多信息),该值基本上是一个从0到255的值,代表一个字符-因此它实际上不会出现错误,但我不认为那是你的意思。 These are the only problems with these code, I tried it and it worked fine... Just change the for loop. 这些是这些代码的唯一问题,我尝试了一下,效果很好...只需更改for循环即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM