fgets和sscanf in c
我有一个文件正在尝试提取整数和字符。
10 20
0 0 #
1 0 |
2 0 |
3 0 |
3 1 -
3 2 -
3 3 -
3 4 >
我当前的代码是:
while(fgets(line, sizeof(line), snakeFile) != NULL)
{
if(sscanf(line, "%d %d", &ROW, &COL) == 2)
{
printf("ROW: %d, COL: %d\n", ROW, COL);
}
else
{
sscanf(line, "%d %d %c\n", &xPos, &yPos, &zType);
printf("xPos: %d, yPos: %d, zType: %c\n", xPos, yPos, zType);
}
}
但是我将其作为输出:
ROW: 10, COL: 20
ROW: 0, COL: 0
ROW: 1, COL: 0
ROW: 2, COL: 0
ROW: 3, COL: 0
ROW: 3, COL: 1
ROW: 3, COL: 2
ROW: 3, COL: 3
ROW: 3, COL: 4
我希望它获得前两个整数并将其存储在行和col中,然后将其余的线分别存储在XPO,YPOS和ZTYPE中。
I have a file I'm trying to extract the integers and characters out of.
10 20
0 0 #
1 0 |
2 0 |
3 0 |
3 1 -
3 2 -
3 3 -
3 4 >
My current code is:
while(fgets(line, sizeof(line), snakeFile) != NULL)
{
if(sscanf(line, "%d %d", &ROW, &COL) == 2)
{
printf("ROW: %d, COL: %d\n", ROW, COL);
}
else
{
sscanf(line, "%d %d %c\n", &xPos, &yPos, &zType);
printf("xPos: %d, yPos: %d, zType: %c\n", xPos, yPos, zType);
}
}
However I get as the output:
ROW: 10, COL: 20
ROW: 0, COL: 0
ROW: 1, COL: 0
ROW: 2, COL: 0
ROW: 3, COL: 0
ROW: 3, COL: 1
ROW: 3, COL: 2
ROW: 3, COL: 3
ROW: 3, COL: 4
I would like it to get the first two integers and store it in ROW and COL and then the rest of the lines underneath it stored in xPos, yPos and zType respectively.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在使用条件
来确定您当前是否正在处理第一行。但是,这种情况无法确定这一点,因为所有线路,而不仅仅是第一个条件都将是正确的。功能
scanf
将成功匹配2个数字,然后忽略该行的其余部分。您可以做的一件事是将该行更改为
和
else> else
block,并交换的内容。这应该起作用,因为除第一条线以外的所有线路都将是正确的。
但是,一个更简单的解决方案是不要尝试处理循环内的第一行,而是要在循环外处理该行:
请注意,您可能必须添加
#include< stdlib.h>
能够调用函数退出
。You appear to be using the condition
to determine whether you are currently processing the first line or not. However, this condition is unable to determine this, because that condition will be true with all lines, not just the first one. The function
scanf
will successfully match 2 numbers and will then ignore the rest of the line.One thing you could do would be to change that line to
and to swap the contents of the
if
and theelse
block. This should work, because this condition will be true for all lines except the first one.However, a simpler solution would be to not attempt to handle the first line inside the loop, but to instead handle that line outside the loop:
Note that you may have to add
#include <stdlib.h>
to be able to call the functionexit
.sscanf(行,“%d%d”,&amp; row,&amp; col)== 2
匹配OP样本的每一行,每行以2个数字开始。如果扫描全部线,则真正的整洁方法使用
“%n”
来存储扫描的偏移量。即使文件的最后一行缺少
'\ n'
,这也可以很好地工作。如果扫描包含所需的后缀,这也很好。
考虑输入:
然后使用
”扫描&lt;%d%d%d%c&gt;%n”
sscanf(line, "%d %d", &ROW, &COL) == 2
matches every line of OP's sample as each line begins with 2 numbers.A real neat way of testing if the entire line was scanned uses
" %n"
to store the offset of the scan.This works well even if the last line of the file lacked a
'\n'
.This also works well if the scan included a required suffix.
Consider input like:
Then scan with
" < %d %d %c > %n"