所有这一切可能是一个真正简单的,但我错过了一些东西,希望你能提供帮助。好的,这是我的问题,我可以说得很简单。
我readfile
在使用 USB 设备后返回一个缓冲区。这一切正常,我可以通过使用这样的循环来很好地输出缓冲区
for (long i=0; i<sizeof(buffer); i++) //for all chars in string
{
unsigned char c = buffer[i];
switch (Format)
{
case 2: //hex
printf("%02x",c);
break;
case 1: //asc
printf("%c",c);
break;
} //end of switch format
}
当我使用 text ( %c
) 版本时,我可以按照我预期的方式在屏幕上看到缓冲区中的数据。但是我的问题是当我使用sscanf
. 我strstr
用来搜索缓冲区中的一些键并用于sscanf
检索其数据。然而,sscanf
失败。可能是什么问题呢?
下面是我用来扫描缓冲区的代码示例,它适用于这个独立版本。上面代码中的缓冲区部分无法读取。即使我可以看到它printf
。
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main ()
{
// in my application this comes from the handle and readfile
char buffer[255]="CODE-12345.MEP-12453.PRD-222.CODE-12355" ;
//
int i;
int codes[256];
char *pos = buffer;
size_t current = 0;
//
while ((pos=strstr(pos, "PRD")) != NULL) {
if (sscanf(pos, "PRD - %d", codes+current))
++current;
pos += 4;
}
for (i=0; i<current; i++)
printf("%d\n", codes[i]);
system("pause");
return 0;
}
谢谢