c - 每行读取一行,并使用 fgets() 和 sscanf() 将字符串评估为坐标

标签 c file-io scanf point-in-polygon

我正在尝试使用 fgets 和 sscanf 读取多行不同长度的顶点。

(1,6),(2,6),(2,9),(1,9)
(1,5)

我的程序进入第一个顶点的无限循环。

    char temp3[255];
    while(fgets(temp3, 255, fp)!= NULL){
        printf("Polygon %d: ", polycount);
        while(sscanf(temp3, "(%d,%d)", &polygonx[polycount][vertcount], &polygony[polycount][vertcount]) != EOF){
            sscanf(temp3, ",");
            printf("(%d,%d),",polygonx[polycount][vertcount], polygony[polycount][vertcount]);
            vertcount++;
        }
        vertcounts[polycount] = vertcount;
        vertcount = 0;
        polycount++;
    }

我必须能够将顶点的 x 和 y 值馈送到多边形数组中,所以我坚持使用 sscanf。我也遇到了问题,因为我无法在互联网上找到每行扫描不同数量元素的任何东西。

最佳答案

这是因为

while(sscanf(temp3, "(%d,%d)", 
  &polygonx[polycount][vertcount], &polygony[polycount][vertcount]) != EOF)
 {
 }

永远不会是 true 我想,因为 scanf() 返回成功扫描的参数数量,我会这样做

while(sscanf(temp3, "(%d,%d)", 
  &polygonx[polycount][vertcount], &polygony[polycount][vertcount]) == 2)
 {
 }

你的代码不能运行是因为它不满足sscanf()返回EOF的条件,以下摘自文末引用的手册页

The value EOF is returned if the end of input is reached before either the first successful conversion or a matching failure occurs. EOF is also returned if a read error occurs, in which case the error indicator for the stream (see ferror(3)) is set, and errno is set to indicate the error.

所以如果在第一次转换成功之前输入或者出现匹配失败,那么看起来你没有到达终点,根据文件的内容,这是有道理的。当然,第二部分仅适用于文件流。

而不是 sscanf(temp3, ",") 它不会按照你的想法去做,你可以这样做

next = strchr(temp3, ',');
if (next != NULL)
    temp3 = next + 1;
else
    /* you've reached the end here */

这是关于如何解析这个文件的建议

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

int
main(void)
{
    const char temp3[] = "(1,6),(2,6),(2,9),(1,9)\n(1,5)";
    char *source;
    int x, y;
    int count;
    source = temp3;
    while (sscanf(source, "(%d,%d)%*[^(]%n", &x, &y, &count) == 2)
    {
        /* this is just for code clarity */
        polygonx[polycount][vertcount] = x;
        polygony[polycount][vertcount] = y;
        /* Process here if needed, and then advance the pointer */
        source += count;
    }
    return 0;
}

"%n" 说明符捕获到目前为止扫描的字符数,因此您可以使用它来将指针前进到源字符串中扫描的 las 位置。

并且 "%*[^(" 将跳过所有字符,直到找到下一个 '('

请引用sscanf(3)有关 "%n" 说明符和 %[ 说明符的更多信息。

关于c - 每行读取一行,并使用 fgets() 和 sscanf() 将字符串评估为坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32760963/

相关文章:

python - 在Python(最好是py3k)上使用sscanf?

c - fscanf 转结构体数组

c - 标准 C 函数在第一次调用时速度较慢,如何正确解决这个问题?

c - 如何使用libudev获取USB端口的端口信息?

c - 关于C编程的问题

c++ - 在 C++ 中读入数组

c++ - 重载 operator<< 以处理字符串

C 判断用户输入是否为整数

python - 尝试打开()文件时出现类型错误: 'newline' is an invalid keyword argument for this function,

c - 循环重复scanf和printf