踩过可变长度的整数行

时间:2014-05-24 04:48:56

标签: c input scanf

我正在尝试阅读一个类似这样的文件......

52 12 123 1
4 2 11 
9 88 1 23 42

随着每一行改变,文件继续 我知道如何使用带有fscanf的循环单独读取每个数字,但我想读取第一个数字作为树中的父项,其余部分是其子项。所以这将使3棵树,52,4和9成为父母。我应该如何阅读剩下的行并将每个int添加到正确的树中?我在c编码。

2 个答案:

答案 0 :(得分:0)

我认为条件循环在这种情况下很有用。 基本流程就像这样

do
{
   Read the first integer using fscanf

   if next character is not End of Line i.e. \n or EOF 
   {        
         read rest of line till end of Line  
         store the numerical into a temp array
         read from array and append them into tree

   }
} while (EOF)

答案 1 :(得分:0)

逐行读取然后将行输入解析为整数可以提供更多的灵活性。使用指针逐步执行直到找到空终止符可以容纳每行不同数量的整数。以下是对提供的示例输入做得很好的解决方案的快速破解。代码是:

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

int
main (int argc, char *argv[]) {

    if (argc < 2 ) {
        fprintf (stderr, "Error: insufficient input, usage: %s char *fname\n", argv[0]);
        return 1;
    }

    FILE *file;
    char *lineptr = NULL;
    char *sptr = NULL, *eptr = NULL;
    size_t n = 0;
    ssize_t len = 0;
    int cnt = 0;
    int parent = 1;

    if ((file = fopen (argv[1], "r")) == NULL) {
        fprintf (stderr, "Error: file open failed for '%s'\n", argv[1]);
        return 1;
    }

    while ((len = getline (&lineptr, &n, file)) != -1 ) {
        /* strip newline */
        if (*(lineptr+len-1) == '\n') {
            *(lineptr+len-1) = '\0';
            len -= 1;
        }

        /* test output of line to parse */
        printf ("\nLine [%2d]: \"%s\"\n\n", cnt, lineptr);

        /* parse line into integers */
        parent = 1;
        sptr = NULL;
        eptr = lineptr;
        while (*eptr != '\0') {
            if (*eptr != ' ' && sptr == NULL) {
                sptr = eptr;
                eptr++;
            }
            while (*eptr != ' ' && *eptr != '\0') {
                eptr++;
            }

            if (*eptr == '\0') {
                printf ("  add to tree: %d\n", atoi (sptr));
                break;
            }

            if (*eptr == ' ' && sptr != NULL) {
                *eptr = '\0';
                if (parent) {
                    printf ("  add to tree: %d  (parent)\n", atoi (sptr));
                    parent = 0;
                } else {
                    printf ("  add to tree: %d\n", atoi (sptr));
                }
                *eptr = ' ';
                sptr = NULL;
            }

            eptr++;
        }
        cnt++;
    }

    printf ("\nTotal lines parsed: %d\n\n", cnt);

    return 0;
}

样本数据的输出是:

Line [ 0]: "   52 12 123 1"

add to tree: 52  (parent)
add to tree: 12
add to tree: 123
add to tree: 1

Line [ 1]: "   4 2 11"

add to tree: 4  (parent)
add to tree: 2
add to tree: 11

Line [ 2]: "   9 88 1 23 42"

add to tree: 9  (parent)
add to tree: 88
add to tree: 1
add to tree: 23
add to tree: 42

Total lines parsed: 3

我确信有一种更快的方法可以做到这一点,但简单地通过带有指针的行来消除调用字符串函数所需的相当多的开销。我不知道你正在考虑什么类型的树,但是这段代码提供了一种简单的方法来添加当前printfs所在的插入例程。