- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在此代码中使用 sscanf
while(totalByte < sizeof(rdata))
{
read(client_sockfd, &buffer[totalByte],1);
printf("bytes[%d] : %x \n", totalByte, buffer[++totalByte]);
}
使用这段代码,我得到了这样的结果
客户端发送1 1 +
bytes[0] : 0
bytes[1] : 0
bytes[2] : 0
bytes[3] : 1
bytes[4] : 0
bytes[5] : 0
bytes[6] : 0
bytes[7] : 1
bytes[8] : 2b
bytes[9] : 0
bytes[10] : 0
bytes[11] : 0
bytes[12] : 0
bytes[13] : 0
bytes[14] : 0
bytes[15] : 0
bytes[16] : 0
bytes[17] : 0
bytes[18] : 0
bytes[19] : 0
得到结果
然后我使用sscanf方法
sscanf(buffer,"%d%d%c" ,&rdata.left_num, &rdata.right_num, rdata.op);
printf("%d %c %d \n", ntohl(rdata.left_num),rdata.op,ntohl(rdata.right_num));
但是当打印rdata(结构体)的值时,得到一个0值(初始值)。
0 0
我知道 sscanf 方法分割字符串并插入一个值
对我有什么误解吗?
这就是我使用的结构
struct cal_data
{
int left_num;
int right_num;
char op;
int result;
int error;
};
最佳答案
我认为这没有达到您想要的效果:
printf("bytes[%d] : %x \n",totalByte, buffer[++totalByte]);
如果这有效,那么它就是巧合。你不想依赖巧合而不是逻辑,对吗?阅读 this question有关未定义行为的更多信息,请参见此处。改为这样,避免省略序列点,以便将来将逻辑塞在一起:
printf("bytes[%d] : %x \n",totalByte, (unsigned int) buffer[totalByte]);
totalByte++;
then i use sscanf method
sscanf(buffer,"%d%d%c" ,&rdata.left_num, &rdata.right_num, rdata.op);
你的错误检查在哪里?与大多数标准 C 函数一样,您的代码应该检查 sscanf 的返回值,以确保它提取了您想要的信息量。如何确定 sscanf 成功处理了两个十进制数字序列和一个字符?使用这个:
int n = sscanf(buffer,"%d%d%c" ,&rdata.left_num, &rdata.right_num, rdata.op);
if (n == 3) {
/* sscanf extracted and assigned three values;
* One for each of the format specifiers,
* and variables you passed in. Success! */
printf("%d %c %d \n", ntohl(rdata.left_num),rdata.op,ntohl(rdata.right_num));
}
else {
/* sscanf failed to extract some values from the string,
* because the string wasn't correctly formatted */
puts("Invalid input to sscanf");
}
...现在你就会看到问题了! sscanf 失败!
正如其他人所指出的,字符串在第一个 '\0'
(或 0)字节处终止。您传递给 sscanf 的指针指向一个空字符串。 sscanf 无法从空字符串中提取任何您想要的信息。
关于c - 如何在 C 中使用 sscanf() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15846203/
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
我在程序中遇到了以下行。通过阅读手册,我知道 sscanf 从 argv[2] 指向的任何地方复制,但我不确定为什么格式被指定为 %d,同时指定为 %c (我见过其他双引号中包含更多格式说明符的示例)
我可以使用字符指针 (char *) 作为 sscanf() 函数中的输出字符串吗?考虑到我不知道字符串的长度(这就是我使用指针的原因)。 char *name; sscanf(data, "Name
我有一个表单的输入字符串 char *s = "one.two three" 我想把它分成 3 个字符串变量。我在做 sscanf(s, "%s.%s %s", one, two, three); 但
我一直在尝试让 sscanf 使用字符类识别一种相当简单的格式。我注意到,当我为 sscanf 提供 char* 以匹配字符类时,它也会覆盖前一个字节,就好像它需要一个指向 2 个字节的指针一样。 我
我正在将 MAC 地址的字符串表示形式转换为 UINT8 的数组s 定义为 unsigned char 。我很好奇为什么sscanf()当我读入 UINT8 数组时,将读取全 0当我读入常规 32 位
我有一个包含空格和标签的字符串,例如: sp|P02671|FIBA_HUMAN Fibrinogen alpha chain OS=Homo sapiens GN=FGA PE=1 SV=2 我只想
我正在尝试在 C 中使用 sscanf 函数,但它不读取格式是必需的,我已经阅读了该函数的文档并遵循了示例,但它对我来说仍然效果不佳,因此我想要一些建议.. int main() { long i
我需要在c中解析一个格式为“foo=%d”的字符串,我需要检查格式是否正确并读取int值。 我的初始代码是: int foo_set = 0; int foo; if (sscanf(text, "f
在下面的代码中,我想读取十六进制字符串 'a' 中的前 2 个字符,使用 sscanf 将它们转换为相应的字节值并将结果放入 'b'。不应对“a”执行任何修改。 #include #include
我试图找出我应该给 sscanf 的模式。 我有一个字符串 abcde(1GB)。我想提取 1 和 GB。我在用 char list[]= "abcde(1GB)"; int memo
我应该得到一个可以是以下任何格式的输入行: 单词 1 和单词 2 之间必须有空格。 单词 2 和单词 3 之间必须有逗号。 单词 2 和单词 3 之间不一定要有空格,但可以有任意数量的空格。 如何分离
最近发现的对 GTA 冗长加载时间 (1) 的解释表明 sscanf() 的许多实现调用 strlen()在他们的输入字符串上为与其他扫描函数( scanf() , fscanf() ...)共享的内
我需要将字符串分成两部分,字符串的第一列是第一部分,字符串的其余部分是第二部分。第一部分需要存储在 first_str 中,第二部分需要存储在 rest_str 中。 我正在使用 sscanf 来实现
我需要从char数组中提取数字,它以hh:mm的格式存储值(示例20:20) 我尝试使用sscanf函数将hh提取为小时变量,将mm提取为分钟变量。 直到时间类似于0number:0number或如果
所以我想知道 sscanf 在遇到像这样的行时是如何工作的: sscanf(input_string, "%s %s %s", cmd1, cmd2, cmd3); 但是假设 input_string
我在以下代码中遇到 sscanf() 问题: void num_check(const char*ps){ char *ps1=NULL; int number=0; unsigned sum_num
谁能解释一下为什么在下面的代码中没有拆分字符串 #include int main(void) { char name[] = "first:last"; char first[20
我正在尝试解析一个 URL,并编写了这段代码: #include int main() { char host[100]; char port[100]; char path
我正在为我正在制作的程序使用 AT 命令,但在使用 sscanf() 函数解析它们时遇到问题。 例如,如果我执行此命令: "AT\r\r\nOK\r\n" 我想:* 在第一次调用时,仅获取“AT”部分
我是一名优秀的程序员,十分优秀!