我如何在空白处使用sscanf?

问题描述 投票:1回答:1

我正在尝试将字符串分成几部分,但是这样做不正确。这是我的代码。

int main(void){
    char point[2][2];
    char *try="Distance P1 P2 // Prints the distance between P1 and P2.";
    sscanf(try,"Distance %s %s",point[0], point[1]);
    printf("point[0]:%s point[1]: %s\n",point[0],point[1] ); 

}

期待此

point[0]:P1 point[1]:P2 

输出

point[0]:P1P2 point[1]: P2 

c string split scanf whitespace
1个回答
2
投票
#include <stdio.h>

int main(void){
    char point[2][3];
    char *try="Distance P1 P2 " ; // Prints the distance between P1 and P2;
    sscanf(try,"Distance %s %s",point[0], point[1]);
    printf("point[0]:%s point[1]: %s\n",point[0],point[1] );
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.