如何获取 sscanf_s 在上次操作中读取了多少字节?
我编写了一个快速内存读取器类,它模拟与 fread
和 fscanf
相同的功能。
基本上,我使用了memcpy并增加了一个内部指针来读取数据,例如fread,但我有一个fscanf_s调用。我使用了 sscanf_s,但它没有告诉我它从数据中读取了多少字节。
有没有办法知道在最后一次操作中读取了多少字节 sscanf_s
以增加字符串读取器的内部指针?谢谢!
编辑:
我正在阅读的示例格式是: |172|44|40|128|32|28|
fscanf 读取得很好,sscanf 也是如此。唯一的原因是,如果是:
|0|0|0|0|0|0|
长度就会不同。我想知道 fscanf 如何知道将文件指针放在哪里,但 sscanf 不知道。
I wrote up a quick memory reader class that emulates the same functions as fread
and fscanf
.
Basically, I used memcpy
and increased an internal pointer to read the data like fread
, but I have a fscanf_s
call. I used sscanf_s
, except that doesn't tell me how many bytes it read out of the data.
Is there a way to tell how many bytes sscanf_s
read in the last operation in order to increase the internal pointer of the string reader? Thanks!
EDIT:
And example format I am reading is:|172|44|40|128|32|28|
fscanf reads that fine, so does sscanf. The only reason is that, if it were to be:
|0|0|0|0|0|0|
The length would be different. What I'm wondering is how fscanf knows where to put the file pointer, but sscanf doesn't.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于
scanf
及其系列,请在格式字符串中使用%n
。它不会读取任何内容,但会导致到目前为止(通过此调用)读取的字符数存储在相应的参数中(需要一个int*
)。With
scanf
and family, use%n
in the format string. It won't read anything in, but it will cause the number of characters read so far (by this call) to be stored in the corresponding parameter (expects anint*
).也许我很傻,但无论如何我都会尝试。从评论中看来,还是存在一些误解。您需要知道字节数。但该方法仅返回读取的字段数或 EOF。
要获取字节数,请使用可以轻松计算的内容,或者在格式字符串中使用大小说明符。否则,除了一一检查字段之外,您将没有机会找出读取了多少字节。另外,您可能的意思是,
在输入“123 456”和“10\t30”上都会成功,它们的长度不同。在这些情况下,无法确定大小,除非将其转换回来。因此:使用固定大小的字段,否则就会被遗忘......
重要提示:请记住,使用
%c
时,这是在其中包含字段分隔符(换行符、制表符和空格)的唯一方法输出。所有其他人都会跳过字段边界,从而更难找到正确的字节数。编辑:
我刚刚从《C++ 完整参考》中读到:
这不正是您所追求的吗?只需将其添加到格式字符串中即可。这是此处确认,但我还没有用 sscanf_s 测试过。
Maybe I´m silly, but I´m going to try anyway. It seems from the comment threads that there's still some misconception. You need to know the amount of bytes. But the method returns only the amount of fields read, or EOF.
To get to the amount of bytes, either use something that you can easily count, or use a size specifier in the format string. Otherwise, you won't stand a chance finding out how many bytes are read, other then going over the fields one by one. Also, what you may mean is that
will succeed on both inputs "123 456" and "10\t30", which has a different length. In these cases, there's no way to tell the size, unless you convert it back. So: use a fixed size field, or be left in oblivion....
Important note: remember that when using
%c
it's the only way to include the field separators (newline, tab and space) in the output. All others will skip the field boundaries, making it harder to find the right amount of bytes.EDIT:
From "C++ The Complete Reference" I just read that:
Isn't that precisely what you were after? Just add it in the format string. This is confirmed here, but I haven't tested it with sscanf_s.
来自 MSDN:
每个函数都会返回成功转换和分配的字段数量;返回值不包括已读取但未分配的字段。返回值 0 表示未分配任何字段。如果出现错误或在第一次转换之前到达字符串末尾,则返回值为 EOF。
From MSDN:
Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. A return value of 0 indicates that no fields were assigned. The return value is EOF for an error or if the end of the string is reached before the first conversion.