t*i 发帖数: 72 | 1 我有这样一个string
ORDCF - SA ABC KOPS M1 "SP C 0806 250.0" - BUY 4.50 5 15 20 0 CLS
我现在要单独取出SA, ABC, "SP C 0806 250.0", 4.50, 5, 15, 20
请问在C语言中如何实现。
非常感谢 | l***8 发帖数: 149 | 2 char s1[256], s2[256], s3[256];
float f1;
int n1, n2, n3;
sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3
, &f1, &n1, &n2, &n3); | t*i 发帖数: 72 | 3 非常感谢, 刚才查了一下似乎strtok()也可以用, 不知道那种效率比较高一些,谢谢。
s3
【在 l***8 的大作中提到】 : char s1[256], s2[256], s3[256]; : float f1; : int n1, n2, n3; : sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3 : , &f1, &n1, &n2, &n3);
| t*i 发帖数: 72 | 4 I would like divide s3 in 4 strings like SP,C, 0806, 250.0.
I tried to use
char *s3_1;
char *s3_2;
char *s3_3;
float s4_4;
sscanf(s3,"%s%s%s%f,s3_1,s3_2,s3_3,s3_4);
But it shows Segmentation fault when I run it.
could you tell me what's correct way to do it? Thank you very much
s3
【在 l***8 的大作中提到】 : char s1[256], s2[256], s3[256]; : float f1; : int n1, n2, n3; : sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3 : , &f1, &n1, &n2, &n3);
| l***8 发帖数: 149 | 5 you should pass in pointers in sscanf of integers and floats
"&s3_4"
【在 t*i 的大作中提到】 : I would like divide s3 in 4 strings like SP,C, 0806, 250.0. : I tried to use : char *s3_1; : char *s3_2; : char *s3_3; : float s4_4; : sscanf(s3,"%s%s%s%f,s3_1,s3_2,s3_3,s3_4); : But it shows Segmentation fault when I run it. : could you tell me what's correct way to do it? Thank you very much :
| k**f 发帖数: 372 | 6
did you allocate space for s3_1, s3_2, s_3 before calling sscanf()? As it is
now, these pointers to char are wild pointers pointing to random locations.
So you get the error when you call sscanf() with them.
One way to get rid of the error is to declare s3_1, s3_2 and s3_3 as
character array, assuming the strings are no longer than 31 characters:
char s3_1[32], s3_2[32], s3_3[32];
Also, the previous post correctly pointed out that you should use &s4_4 as
the last argument for the call.
【在 t*i 的大作中提到】 : I would like divide s3 in 4 strings like SP,C, 0806, 250.0. : I tried to use : char *s3_1; : char *s3_2; : char *s3_3; : float s4_4; : sscanf(s3,"%s%s%s%f,s3_1,s3_2,s3_3,s3_4); : But it shows Segmentation fault when I run it. : could you tell me what's correct way to do it? Thank you very much :
| t*i 发帖数: 72 | 7 C++能在代码的任意地方定义变量。似乎C不可以,只能定义在开头。原先也犯了这个错
误。
is
locations.
【在 k**f 的大作中提到】 : : did you allocate space for s3_1, s3_2, s_3 before calling sscanf()? As it is : now, these pointers to char are wild pointers pointing to random locations. : So you get the error when you call sscanf() with them. : One way to get rid of the error is to declare s3_1, s3_2 and s3_3 as : character array, assuming the strings are no longer than 31 characters: : char s3_1[32], s3_2[32], s3_3[32]; : Also, the previous post correctly pointed out that you should use &s4_4 as : the last argument for the call.
| f*******y 发帖数: 988 | 8 按新标准可以
【在 t*i 的大作中提到】 : C++能在代码的任意地方定义变量。似乎C不可以,只能定义在开头。原先也犯了这个错 : 误。 : : is : locations.
| s****n 发帖数: 700 | 9 有人能稍微解释一下么?
s3
【在 l***8 的大作中提到】 : char s1[256], s2[256], s3[256]; : float f1; : int n1, n2, n3; : sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3 : , &f1, &n1, &n2, &n3);
| H***a 发帖数: 735 | | | | s****n 发帖数: 700 | | F********g 发帖数: 475 | 12 借题请教
如果需要处理这样一个GPS数据
"$GPGGA,210917.000,3021.37564,N,09107.50991,W,0,00,99.0,003.75,M,-25.9,M,,*
65"
需要抓出210917.000
3021.37564
N
09107.50991
W
0
用SSCANF如何处理?
【在 l***8 的大作中提到】 : char s1[256], s2[256], s3[256]; : float f1; : int n1, n2, n3; : sscanf(input_string, "%*s%*s%s%s%*[^\"]\"%[^\"]\"%*s%*s%f%d%d%d", s1, s2, s3 : , &f1, &n1, &n2, &n3);
| A******g 发帖数: 612 | 13 读到逗号的符号是%[^','],
比如
sscanf(str,"%[^','],%[^','] ...",para1,para2);
个人不喜欢parse逗号,我宁愿把逗号换成空格,比如 | F********g 发帖数: 475 | 14 Thanks, I tried
sscanf(g_string[g_i-1],"%*[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%*s",gps_
data.utc,gps_data.lat,gps_data.lat_s,gps_data.lon,gps_data.lon_s,gps_data.qi
);
and worked.Not sure how safe it is though. | F********g 发帖数: 475 | 15 Thanks, I tried
sscanf(g_string[g_i-1],"%*[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%*s",gps_
data.utc,gps_data.lat,gps_data.lat_s,gps_data.lon,gps_data.lon_s,gps_data.qi
);
and worked.Not sure how safe it is though. |
|