代码之家  ›  专栏  ›  技术社区  ›  Jérôme

用C语言分析整数行

  •  1
  • Jérôme  · 技术社区  · 16 年前

    我有这样一个输入文件:

    1 3 9 13 23 25 34 36 38 40 52 54 59 
    2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114 
    2 4 9 15 23 27 34 36 63 67 76 85 86 90 93 99 108 115 
    1 25 34 36 38 41 52 54 59 63 67 76 85 86 90 93 98 107 113 
    2 3 9 16 24 28 
    2 3 10 14 23 26 34 36 39 41 52 55 59 63 67 76 
    

    由一个空格分隔的不同整数行。

    我想在数组中解析它们,并用一个标记分隔每一行 -1 .

    这里是我现有的代码,它在scanf循环上循环(因为scanf不能从给定的位置开始)。

    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char **argv) {
    
      if (argc != 4) {
        fprintf(stderr, "Usage: %s <data file> <nb transactions> <nb items>\n", argv[0]);
        return 1;
      }
      FILE * file;
      file = fopen (argv[1],"r");
      if (file==NULL) {
        fprintf(stderr, "Error: can not open %s\n", argv[1]);
        fclose(file);
        return 1;
      }
      int nb_trans = atoi(argv[2]);
      int nb_items = atoi(argv[3]);
      int *bdd = malloc(sizeof(int) * (nb_trans + nb_items));
      char line[1024];
      int i = 0;
    
      while ( fgets(line, 1024, file) ) {
        int item;
        while ( sscanf (line, "%d ", &item )){
          printf("%s %d %d\n", line, i, item);
          bdd[i++] = item;
        }
        bdd[i++] = -1;
      }
    
      for ( i = 0; i < nb_trans + nb_items; i++ ) {
        printf("%d ", bdd[i]);
      }
      printf("\n");
    }
    
    4 回复  |  直到 16 年前
        1
  •  6
  •   Michael Dorgan    16 年前

    你有很多选择,但我通常会这么做:

    使用fgets()将输入文件作为文本文件读入,即作为一组字符串读入。它将一直读取,直到达到换行符或EOF。使用一个字符串标记器函数,扫描每一行读取的空格,并返回空格前的子字符串。现在有了整数的字符串表示形式。如果愿意,可以将其解析为实际的int,或者将子字符串本身存储在数组中。如果你真的把它换成int,如果它太大,你需要小心溢出。

        2
  •  1
  •   JAB    16 年前

    然后创建数组。

    接下来,使用sscanf或其他方法来解释循环中字符串中的整数,并将它们添加到数组中正确的位置,直到所有整数(包括-1)都被解释完毕。

        3
  •  0
  •   Community Mohan Dere    9 年前

    好的,我找到了解决办法,对不起,噪音太大了,我应该多找些。。。

    reading unknown number of integers from stdin (C)

    用这个代替我的scanf循环:

      while ( fgets(line, 1024, file) ) {
        int item;
        for (p = line; ; p = e) {
            item = strtol(p, &e, 10);
            if (p == e)
                break;
            bdd[i++] = item;
        }
        bdd[i++] = -1;
      }
    
        4
  •  0
  •   paxdiablo    16 年前

    fgets ,然后使用 sscanf 处理该行上的每个inetegr。

    它有基本的错误检查,但它还没有测试坏数据(行非数字),但它应该是一个良好的开端。只需更换 printf 包含将每个数字附加到数组的代码的语句:

    #include <stdio.h>
    #include <string.h>
    #include <errno.h>
    
    int main (void) {
        char line[1000];
        FILE *fIn;
        char *str;
        int val, num;
    
        // Open input file and process line by line.
    
        if ((fIn = fopen ("infile.txt", "r")) == NULL) {
            fprintf (stderr, "Cannot open infile.txt, errno = %d\n", errno);
            return 1;
        }
    
        while (fgets (line, sizeof (line), fIn) != NULL) {
            // Check if line was too long.
    
            if (line[strlen (line) - 1] != '\n') {
                fprintf (stderr, "Line too long: [%s...]\n", line);
                fclose (fIn);
                return 1;
            }
    
            // Oyput the line and start processing it.
    
            printf ("%s   ", line);
            str = line;
    
            // Skip white space and scan first inetegr.
    
            while (*str == ' ') str++;
    
            num = sscanf (str, "%d", &val);
    
            // Process the integer if it was there.
    
            while ((num != 0) && (num != EOF)) {
                // Print it out then skip to next.
    
                printf ("[%d] ", val);
                while ((*str != ' ') && (*str != '\0')) str++;
                while (*str == ' ') str++;
                num = sscanf (str, "%d", &val);
            }
    
            // -1 for line separator.
    
            printf ("[%d]\n", -1);
        }
    
        // Close input file and exit.
    
        fclose (fIn);
    
        return 0;
    }
    

    1 3 9 13 23 25 34 36 38 40 52 54 59
       [1] [3] [9] [13] [23] [25] [34] [36] [38] [40] [52] [54] [59] [-1]
    2 3 9 14 23 26 34 36 39 40 52 55 59 63 67 76 85 86 90 93 99 108 114
       [2] [3] [9] [14] [23] [26] [34] [36] [39] [40] [52] [55] [59] [63] [67] [76] [85] [86] [90] [93] [99] [108] [114] [-1]
    2 4 9 15 23 27 34 36 63 67 76 85 86 90 93 99 108 115
       [2] [4] [9] [15] [23] [27] [34] [36] [63] [67] [76] [85] [86] [90] [93] [99] [108] [115] [-1]
    1 25 34 36 38 41 52 54 59 63 67 76 85 86 90 93 98 107 113
       [1] [25] [34] [36] [38] [41] [52] [54] [59] [63] [67] [76] [85] [86] [90] [93] [98] [107] [113] [-1]
    2 3 9 16 24 28
       [2] [3] [9] [16] [24] [28] [-1]
    2 3 10 14 23 26 34 36 39 41 52 55 59 63 67 76
       [2] [3] [10] [14] [23] [26] [34] [36] [39] [41] [52] [55] [59] [63] [67] [76] [-1]
    
    推荐文章