简体   繁体   English

如何获取sscanf_s在其上一次操作中读取的字节数?

[英]How can I get how many bytes sscanf_s read in its last operation?

I wrote up a quick memory reader class that emulates the same functions as fread and fscanf . 我编写了一个快速内存阅读器类,它模拟与freadfscanf相同的功能。

Basically, I used memcpy and increased an internal pointer to read the data like fread , but I have a fscanf_s call. 基本上,我使用memcpy并增加了一个内部指针来读取像fread这样的数据,但我有一个fscanf_s调用。 I used sscanf_s , except that doesn't tell me how many bytes it read out of the data. 我使用了sscanf_s ,除了它没有告诉我从数据中读出了多少字节。

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? 有没有办法告诉sscanf_s在上一次操作中读取了多少字节,以增加字符串阅读器的内部指针? Thanks! 谢谢!

EDIT: 编辑:

And example format I am reading is: |172|44|40|128|32|28| 我正在阅读的示例格式是: |172|44|40|128|32|28|

fscanf reads that fine, so does sscanf. fscanf读得很好,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. 我想知道的是fscanf如何知道放置文件指针的位置,但sscanf不知道。

With scanf and family, use %n in the format string. 使用scanf和family,在格式字符串中使用%n 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 an int* ). 它不会读取任何内容,但它会导致到目前为止读取的字符数(通过此调用)存储在相应的参数中(需要int* )。

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. 但该方法仅返回读取的字段数量或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 另外,你的意思是

sscanf_s(source, "%d%d"...) 

will succeed on both inputs "123 456" and "10\\t30", which has a different length. 将在“123 456”和“10 \\ t30”两个输入上成功,这两个输入具有不同的长度。 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. 重要说明:请记住,在使用%c它是在输出中包含字段分隔符(换行符,制表符和空格)的唯一方法。 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: 从“C ++完整参考”我刚读到:

%n Receives an integer value equal to the nubmer of characters read so far %n接收一个等于到目前为止读取的字符的nubmer的整数值

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. 在这里得到了证实 ,但我没有用sscanf_s测试它。

From MSDN: 来自MSDN:

sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l  

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. 返回值0表示未分配任何字段。 The return value is EOF for an error or if the end of the string is reached before the first conversion. 如果出现错误,或者在第一次转换之前到达字符串的结尾,则返回值为EOF。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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