[英]Parsing a string separated by Commas using MPLAB Compiler?
I am parsing a string separated by commas and printing the values but for some reason this code only prints the first value if I run it on MPLAB C Compiler. 我正在解析一个用逗号分隔的字符串并打印值,但是由于某些原因,如果我在MPLAB C编译器上运行该代码,则该代码仅显示第一个值。 However, if I run this code on Codeblocks, it prints out all the values.
但是,如果我在代码块上运行此代码,它将打印出所有值。
MPLAB OUTPUT MPLAB输出
2 2
CODEBLOCKS OUTPUT 代码块输出
2 2
100 100
200 200
100 100
Code: 码:
char somestr[] ="2,0100,0200,0100";
char *pt;
int a;
pt = strtok (somestr,",");
while (pt != NULL)
{
a = atoi(pt);
printf("%d\n", a);
pt = strtok (NULL, ",");
}
I would like the output to be like this 我希望输出是这样的
2 2
0100 0100
0200 0200
0100 0100
So what is really going on ? 那么到底是怎么回事? Thanks!
谢谢!
I'm wondering how you got 0100
and 0200
(using the %d
specifier, you should get 100
and 200
, and you do ), but by running this code, all the output should definitely be printed. 我想知道如何得到
0100
和0200
(使用%d
说明符,应该得到100
和200
, 然后得到),但是通过运行此代码,肯定应该打印所有输出。 The code seems fine. 该代码似乎很好。 Possible reasons why this may not be the case:
可能并非如此的可能原因:
Edit: so you want the output to be zero-padded. 编辑:所以您希望输出被零填充。 For that, use the
%04d
format specifier when printf()
ing the variable. 对于这一点,使用
%04d
时格式说明printf()
荷兰国际集团的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.