简体   繁体   中英

fscanf() fails to read number if preceeding string was empty

This code

fscanf(f, "%[^|]|%d", text, &num)

Will work if text is longer than 0 bytes, for example the file contains:

testtext|89

But if the text is 0 bytes long like here:

|89

then the number read by fscanf will be zero! What's wrong?

According to this page: http://www.cplusplus.com/reference/cstdio/fscanf/

at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there.

In such cases, its more preferable to use strtok() and use '|' as the delimiter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM