首页 > 解决方案 > 从 C 中的文件中读取一行并提取输入的数量

问题描述

我有一个文件input.dat。在这个文件中,有 3 行:

1 2 3
5 7 10 12
8 9 14 13 15 17

我将使用 C 读取三行之一,并返回元素的数量。例如,我想将第 2 行读5 7 10 12入内存,并返回第 2 行中的值的个数,即4. 我的代码如下...

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

#define STRING_SIZE 2000

int main() {
    FILE *fp = fopen("in.dat", "r");
    char line[STRING_SIZE];
    int lcount = 0, nline = 1, sum = 0, number;

    if (fp != NULL) {
        while (fgets(line, STRING_SIZE, fp) != NULL) {
            if (lcount == nline) {
                while (sscanf(line, "%d ", &number)) {
                    sum++;
                }
                break;
            } else {
                lcount++;
            }
        }
        fclose(fp);
    }
    exit(0);
}

当我运行这段代码时,它永远不会像死循环一样停止。这里有什么问题?

标签: cscanffgetsfread

解决方案


chqrlie答案的更简洁的版本。以字符串开头,因为这就是问题的真正意义 after fgets()

sscanf()不会单步执行字符串,它总是从头开始读取。

strtol()在字符串的开头查找 a long int,忽略初始空格。返回停止扫描的地址。

的手册strtol()说应该检查 errno 是否有任何转换错误。

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

#define STRING_SIZE 2000

int main(void)
{
    char line[STRING_SIZE] = "5 7 10 12";

    char* start = line;
    char* end;

    int count = 0;

    while(1)
    {
        /**
         * strtol() look for long int in beginning of the string
         * Ignores beginning whitespace
         * 
         * start: where to strtol() start looking for long int
         * end: where strtol() stops scanning for long int
         */
        errno = 0; // As strol() manual says

        strtol(start, &end, 0);

        if (errno != 0)
        {
            printf("Error in strtol() conversion.\n");
            exit(0);
        }

        if (start == end) break; // Quit loop

        start = end;
        count++;
    }
    

    printf("count: %d\n", count);

    return 0;
}

推荐阅读