如何用C制表符分隔整数的文本文件中读取? [英] How to read in a text file of tab-separated integers in C?

查看:568
本文介绍了如何用C制表符分隔整数的文本文件中读取?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是制表符分隔的整数的文件(.txt文件),我希望只用C,逐行读取他们进来。所以,说每行有5个整数。我怎样才能做到这一点?

我第一次尝试如下。它只是在一个整数读取,但即使没有工作:

  FILE * FP;
    字符等等[255];
    INT * some_int;
FP = FOPEN(test.txt的,RT);
而(与fgets(胡说,255,FP)!= NULL)
{
    sscanf的(废话,%D,some_int);
    的printf(%d个\\ N,* some_int);
}


解决方案

下面是一个方法没有其他人的建议,不使用的fscanf 所以你可以有理智的错误处理:

 字符缓冲区[BUFSIZE];
为size_t大小= 5;
为int *数据=的malloc(大小* sizeof的*线);如果(行== NULL)错误();而(与fgets(缓冲区,缓冲区的sizeof,FP)
  {
    为size_t I = 0;
    字符*下一=缓冲;
    而(*下一&放大器;&安培;!*下一='\\ n')
      {
        数据[I ++] = strtol将(下,与放大器;下,0);
        //检查错误
      }
  }

基本上,而不是试图用 * scanf函数%D阅读字符,请使用它的功能(可能)调用进行转换:与strtol 。其中, * scanf函数经过串匹配格式字符串,但不会让你保存你的地方,在函数调用,与strtol 不,这是你需要阅读整数任意数量的东西。

我没有写所有code你 - 你要做的硬错误处理。可能的错误包括:


  1. 我==尺寸,在这种情况下,你可以尝试让数据大以的realloc 。另外,还可以通过循环缓冲区,算多少数有先,然后分配很多,所以你并不需要在以后重新分配。

  2. 与fgets 没看过整条生产线(检查前的最后一个字符'\\ 0'的'\\ n')。在这种情况下,你可能会想重新填充缓冲,并保持阅读的数字。在这种情况下,小心 - 你可能需要回去重新计算最后一个数字 - 与fgets 可能已经把它割下来。 (这是一个缺点,使用与fgets

  3. 错误的输入 - 处理不过你喜欢

I have a file of simply tab-separated integers (a .txt file) and I wish to read them in with just C, line by line. So, say each line has 5 integers. How can I accomplish this?

My first attempt was as follows. It was just to read in a single integer, but even that didn't work:

    FILE *fp;
    char blah[255];
    int *some_int;
fp = fopen("test.txt", "rt");
while (fgets(blah, 255, fp) != NULL)
{
    sscanf(blah, "%d", some_int);
    printf("%d\n", *some_int);
}

解决方案

Here's a way no one else suggested, that doesn't use fscanf so you can have sane error handling:

char buffer[BUFSIZE];
size_t size = 5;
int *data = malloc(size * sizeof *line);

if(line == NULL) error();

while(fgets(buffer, sizeof buffer, fp)
  {
    size_t i = 0;
    char *next = buffer;
    while(*next && *next != '\n')
      {
        data[i++] = strtol(next, &next, 0);
        // check for errors
      }
  }

Basically, instead of trying to use *scanf's "%d" to read characters, use the function it (probably) calls to do the conversion: strtol. Where *scanf goes through the string to match the format string but doesn't let you "save your place" in between function calls, strtol does, which is what you need to read an arbitrary number of integers.

I haven't written all your code for you - you have to do the hard error handling. Possible errors include:

  1. i == size, in which case you can try to make data bigger with realloc. Alternately, you could loop through the buffer and count how many numbers there are beforehand, then allocate that many so you don't need to reallocate later.
  2. fgets didn't read the entire line (check that the last character before '\0' is '\n'). In this case you'll probably want to refill the buffer and keep reading numbers. Be careful in this case - you'll likely need to go back and recalculate the last number - fgets might have cut it off. (This is one disadvantage to using fgets.)
  3. Erroneous input - handle however you like.

这篇关于如何用C制表符分隔整数的文本文件中读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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