简体   繁体   English

使用MPLAB编译器解析由逗号分隔的字符串?

[英]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. 我想知道如何得到01000200 (使用%d说明符,应该得到100200然后得到),但是通过运行此代码,肯定应该打印所有输出。 The code seems fine. 该代码似乎很好。 Possible reasons why this may not be the case: 可能并非如此的可能原因:

  • You have some more code that invokes undefined behavior and interferes with this piece of code; 您还有更多的代码可以调用未定义的行为并干扰这段代码。
  • This is not the actual code you're compiling; 这不是您正在编译的实际代码;
  • The terminal or wherever the program prints after having been compiled by the MPLAB compiler has a bug or you just didn't notice that it should be scrolled; 在MPLAB编译器编译后,终端或程序在哪里打印都有错误,或者您根本没有注意到应该滚动它;
  • a compiler or standard library bug? 编译器或标准库错误? (very unlikely...) (不太可能...)

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.

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