如何强制fscanf只查找单个输入行的数据? [英] How to force fscanf to find only data on a single input line?

查看:82
本文介绍了如何强制fscanf只查找单个输入行的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是在FAQ中,请道歉。我看了,但没找到。


在特定程序中,从文件读取的输入应该是:


+ 100 200 name1

- 101 201 name2


通过读取+字符进行解析,然后发送

剩余部分进入fscanf()就像


count = fscanf(fp,"%d%d%s",& first_int,& second_int,& string);


除非输入是伪造的,否则这样可以正常工作。特别是,如果

" name1"停了下来,fscanf愉快地读完了

第一行的EOL并返回 - 从第二行

存储在字符串中。实际上,它将虚假行视为:


+ 100 200 - 101 201 name2


因为它不区分EOL和其他空白区域。

所以数量为3,但错误的字符存储在字符串中。


我想要的是数为2,字符串'的内容为是

undefined。是否有一些魔术格式说明符告诉fscanf()

在查找数据时不要超过EOL?当然,它可以通过

将整行读入缓冲区,然后使用sscanf()来完成。它似乎应该有一种让fscanf()线路识别的方法。


可能吗?


谢谢,


David Mathog

Apologies if this is in the FAQ. I looked, but didn''t find it.

In a particular program the input read from a file is supposed to be:

+ 100 200 name1
- 101 201 name2

It is parsed by reading the + character, and then sending the
remainder into fscanf() like

count = fscanf(fp,"%d %d %s",&first_int,&second_int,&string);

This works fine unless the input is bogus. In particular, if
"name1" is left off, fscanf happily reads past the EOL of the
first line and comes back with "-" from the second line
stored in the string. Effectively it sees the bogus line as:

+ 100 200 - 101 201 name2

since it makes no distinction between EOL and other white space.
So count is 3 but the wrong characters are stored in string.

What I want is for count to be 2 and string''s contents to be
undefined. Is there some magic format specifier that tells fscanf()
not to go past the EOL when looking for data? Sure, it can be done by
reading a whole line into a buffer, and then using sscanf() on that. It
just seems that there should be a way to make fscanf() "line aware".

Possible?

Thanks,

David Mathog

推荐答案



" ; David Mathog < ma **** @ caltech.eduwrote in message

news:fb ********** @ naig.caltech.edu ...

"David Mathog" <ma****@caltech.eduwrote in message
news:fb**********@naig.caltech.edu...

如果这是在FAQ中,请道歉。我看了,但没找到。


在特定程序中,从文件读取的输入应该是:


+ 100 200 name1

- 101 201 name2


通过读取+字符进行解析,然后发送

剩余部分进入fscanf()就像


count = fscanf(fp,"%d%d%s",& first_int,& second_int,& string);


除非输入是伪造的,否则这样可以正常工作。特别是,如果

" name1"停了下来,fscanf愉快地读完了

第一行的EOL并返回 - 从第二行

存储在字符串中。实际上,它将虚假行视为:


+ 100 200 - 101 201 name2


因为它不区分EOL和其他空白区域。

所以数量为3,但错误的字符存储在字符串中。


我想要的是数为2,字符串'的内容为是

undefined。是否有一些魔术格式说明符告诉fscanf()

在查找数据时不要超过EOL?当然,它可以通过

将整行读入缓冲区,然后使用sscanf()来完成。它似乎应该有一种方法可以使fscanf()行识别。
Apologies if this is in the FAQ. I looked, but didn''t find it.

In a particular program the input read from a file is supposed to be:

+ 100 200 name1
- 101 201 name2

It is parsed by reading the + character, and then sending the
remainder into fscanf() like

count = fscanf(fp,"%d %d %s",&first_int,&second_int,&string);

This works fine unless the input is bogus. In particular, if
"name1" is left off, fscanf happily reads past the EOL of the
first line and comes back with "-" from the second line
stored in the string. Effectively it sees the bogus line as:

+ 100 200 - 101 201 name2

since it makes no distinction between EOL and other white space.
So count is 3 but the wrong characters are stored in string.

What I want is for count to be 2 and string''s contents to be
undefined. Is there some magic format specifier that tells fscanf()
not to go past the EOL when looking for data? Sure, it can be done by
reading a whole line into a buffer, and then using sscanf() on that. It
just seems that there should be a way to make fscanf() "line aware".



使用fgets()或Chuck Falconer'的ggets()(谷歌他的名字和ggets找到

)来读一行,然后用sscanf()解析它。


换行被视为空白的事实是公认的设计缺陷

fscanf()。


-

免费游戏和编程好东西。
http://www.personal .leeds.ac.uk /~bgy1mm

Use fgets() or Chuck Falconer''s ggets() (Google his name and ggets to find
it) to read in a line, and then parse it with sscanf().

The fact that newline is treated as whitepace is a recognised design flaw in
fscanf().

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


Malcolm McLean说:


< snip>
Malcolm McLean said:

<snip>

使用fgets()或Chuck Falconer的ggets()(Google将他的名字和ggets发送给

找到它)读取一行,然后用sscanf()解析它。
Use fgets() or Chuck Falconer''s ggets() (Google his name and ggets to
find it) to read in a line, and then parse it with sscanf().



Chuck'的ggets函数至少遇到两个问题,一个是每个调用创建一个必须管理的新缓冲区的
,和另一个

没有任何方法来指定内存的上限

消费。

Chuck''s ggets function suffers from at least two problems, one being
that every call creates a new buffer that must be managed, and another
being the absence of any way to specify an upper limit on memory
consumption.


换行被视为空白的事实是fscanf()中公认的设计

缺陷。
The fact that newline is treated as whitepace is a recognised design
flaw in fscanf().



由谁认可?


-

Richard Heathfield< http:// www.cpax.org.uk>

电子邮件:-www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日

Recognised by whom?

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999




" Richard Heathfield" < rj*@see.sig.invalidwrote in message

news:n6 *************************** *** @ bt.com ...

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:n6******************************@bt.com...

Malcolm McLean说:


< snip>
Malcolm McLean said:

<snip>

>使用fgets()或Chuck Falconer的ggets()(谷歌他的名字和ggets来找到它)来读取一行,然后解析它与sscanf()。
>Use fgets() or Chuck Falconer''s ggets() (Google his name and ggets to
find it) to read in a line, and then parse it with sscanf().



Chuck'的ggets函数至少遇到两个问题,一个是每个调用创建一个必须管理的新缓冲区的
,另一个

没有任何方法来指定内存的上限

消费。


Chuck''s ggets function suffers from at least two problems, one being
that every call creates a new buffer that must be managed, and another
being the absence of any way to specify an upper limit on memory
consumption.



这是对fgets()的一个很大的改进。没有人会试图让大卫崩溃

Mathog的计划是给它一个40亿字符。线,现在,是吗?

It''s a big improvement on fgets(). No one''s going to try to crash David
Mathog''s program by feeding it a 4 billion character .line, now, are they?


>
>

>换行被视为空白的事实是fscanf()中公认的设计缺陷。
>The fact that newline is treated as whitepace is a recognised design
flaw in fscanf().



由谁认可?


Recognised by whom?



我是唯一一个意识到这一点的人吗?我不这么认为,之前已经讨论过了,但我担心我无法引用这些主题。


- -

免费游戏和编程好东西。
http://www.personal.leeds.ac.uk/~bgy1mm

I am I the only one who has realised this? I don''t think so, it has been
discussed before, though I''m afraid I couldn''t reference the threads.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm


这篇关于如何强制fscanf只查找单个输入行的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆