【问题标题】:Reading only selected data from file仅从文件中读取选定的数据
【发布时间】:2013-09-04 21:20:15
【问题描述】:

我有一个包含数据的文件,如示例所示:

    text1 1542 542 6325 6523 3513 323
    text2 1232 354 5412 2225 2653 312
    text3 ....
    ...

我希望我的程序只读取和打印选定的数据,即每第二行只读取第 3 列和第 4 列。所以我需要跳过开头的字符串“text1”,还要跳过该行中的其余列。我该怎么做?

【问题讨论】:

    标签: c file input


    【解决方案1】:

    带有*修饰符的scanf会跳过那部分数据,所以在你的情况下这样做

    fscanf(FilePointer,"%s %*d %*d %d %d", &col3, &col4);
    

    它将按照您的意愿读取第 3 列和第 4 列。

    然后你用常规打印它们

    printf("%d %d\n", col3, col4);
    

    【讨论】:

    • 谢谢,这正是我所需要的。
    【解决方案2】:

    您可以使用 strtok() 来拆分指定分隔符的字符串(在本例中为空格)。

    【讨论】:

      【解决方案3】:

      提示我能想到的最直接的答案:

      1. 跳过一行的最简单方法是调用 fgets 并忽略 得到字符串。

      2. 要从一行中读取七个字符串(不包含空格),请使用:

        scanf("%s %s %s %s %s %s %s\n", s1, s2, s3, s4, s5, s6, s7);
        

        如果您想将一些视为数字,请将%s 替换为%d,例如,将s3 替换为&n3

      3. 要打印任何内容,只需使用printf...

      【讨论】:

        【解决方案4】:

        这里是:

        char buffer [512];
        int lineNum = 0;  // or 1, if you need the odd rather than even numbered lines
        int col3, col4;
        
        while (fgets (buffer, sizeof(buffer), inputFile) {
            if (lineNum % 2) {
                 sscanf (buffer, "%*s %*d %d %d", &col3, &col4)
                 printf ("%d %d\n", col3, col4);
            }  // end if
            lineNum++;
        } // end while
        

        没有尝试过,所以可能需要调试,但这样的东西应该可以工作。此外,这只是打印数字,但在现实生活中,您需要进行任何必要的处理,以防止这些数字被下一次读取覆盖。

        【讨论】:

          【解决方案5】:

          设置一个函数以根据需要跳过行,并设置一个掩码来标识要打印的列。

          int FilterFile(FILE *inf, unsigned Row, unsigned ColumnMask) {
            char buffer[1024];
            unsigned RowCount = 0;
            while (fgets(buffer, sizeof(buffer), inf) != NULL ) {
              if ((++RowCount) % Row)
                continue;
              const char *s = buffer;
              unsigned column = ColumnMask;
              while (*s && column)  {
                int start, end;
                sscanf(s, " %n%*s%n", &start, &end);
                if (1 & column) {
                  printf("%.*s%c", end - start, &s[start], (column == 1) ? '\n' : ' ');
                }
                column >>= 1;
                s = &s[end];
              }
            }
            return 0;
          }
          
          
          
          int main() {
            const char *path = "../src/text.txt";
            FILE *inf = fopen(path, "rt");
            if (inf) {
              unsigned ColumnMask = 0;
              ColumnMask |= 1 << (2-1); // Print column 2
              ColumnMask |= 1 << (3-1); // Print column 3
              unsigned RowSkip = 2; // Print every 2nd row
              FilterFile(inf, RowSkip, ColumnMask);
              fclose(inf);
            }
            return 0;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-05-17
            • 2020-07-01
            • 2012-11-10
            • 1970-01-01
            相关资源
            最近更新 更多