C 中的 fscanf - 如何确定逗号?

发布于 2024-10-27 19:11:27 字数 113 浏览 2 评论 0原文

我正在通过 fscanf() 从文件中读取一组数字,对于每个数字我想将其放入数组中。问题是这些数字是用“,”分隔的,如何确定 fscanf 应该读取多个密码,并且当它在文件中找到“,”时,它会将其保存为整数?谢谢

I am reading set of numbers from file by fscanf(), for each number I want to put it into array. Problem is that thoose numbers are separated by "," how to determine that fscanf should read several ciphers and when it find "," in file, it would save it as a whole number? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初吻给了烟 2024-11-03 19:11:27

这可能是一个开始:

#include <stdio.h>

int main() {
    int i = 0;

    FILE *fin = fopen("test.txt", "r");

    while (fscanf(fin, "%i,", &i) > 0)
        printf("%i\n", i);

    fclose(fin);

    return 0;
}

使用这个输入文件:

1,2,3,4,5,6,
7,8,9,10,11,12,13,

...输出是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13

你到底想做什么?

This could be a start:

#include <stdio.h>

int main() {
    int i = 0;

    FILE *fin = fopen("test.txt", "r");

    while (fscanf(fin, "%i,", &i) > 0)
        printf("%i\n", i);

    fclose(fin);

    return 0;
}

With this input file:

1,2,3,4,5,6,
7,8,9,10,11,12,13,

...the output is this:

1
2
3
4
5
6
7
8
9
10
11
12
13

What exactly do you want to do?

郁金香雨 2024-11-03 19:11:27

我可能会使用类似的东西:

while (fscanf(file, "%d%*[, \t\n]", &numbers[i++]))
    ;

%d 转换数字,“%*[, \t\n]” 读取(但不分配)任何连续的分隔符 - 我将其定义为逗号,空格、制表符、换行符,尽管更改为您认为合适的任何内容相当简单。

I'd probably use something like:

while (fscanf(file, "%d%*[, \t\n]", &numbers[i++]))
    ;

The %d converts a number, and the "%*[, \t\n]" reads (but does not assign) any consecutive run of separators -- which I've defined as commas, spaces, tabs, newlines, though that's fairly trivial to change to whatever you see fit.

夜雨飘雪 2024-11-03 19:11:27

fscanf(file, "%d,%d,%d,%d", &n1, &n2, &n3, &n4); 但如果存在则不起作用数字之间的空格。 这个答案展示了如何做到这一点(因为有不是为此的库函数)

fscanf(file, "%d,%d,%d,%d", &n1, &n2, &n3, &n4); but won't work if there are spaces between numbers. This answer shows how to do it (since there aren't library functions for this)

风透绣罗衣 2024-11-03 19:11:27

Jerry Coffin 的回答很好,但有一些注意事项需要注意:

  1. fscanf 在文件末尾返回一个(负)值,因此循环不会正确终止。

  2. 即使没有读取任何内容,

    i 也会递增,因此它最终会指向数据末尾后面的 1。

  3. 此外,如果格式参数之间留有空格,fscanf 还会跳过所有空格(包括 \t\n

我会选择这样的东西,

int numbers[5000];
int i=0;
while (fscanf(file, "%d %*[,] ", &numbers[i])>0 && i<sizeof(numbers))
{
    i++;
}
printf("%d numbers were read.\n", i);

或者如果你想强制在格式参数之间有一个逗号 。您可以将格式字符串替换为 "%d , "

Jerry Coffin's answer is nice, though there are a couple of caveats to watch for:

  1. fscanf returns a (negative) value at the end of the file, so the loop won't terminate properly.

  2. i is incremented even when nothing was read, so it will end up pointing one past the end of the data.

  3. Also, fscanf skips all whitespace (including \t and \n if you leave a space between format parameters.

I'd go for something like this.

int numbers[5000];
int i=0;
while (fscanf(file, "%d %*[,] ", &numbers[i])>0 && i<sizeof(numbers))
{
    i++;
}
printf("%d numbers were read.\n", i);

Or if you want to enforce there being a comma between the numbers you can replace the format string with "%d , ".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文