avatar
一个c语言的问题# Programming - 葵花宝典
t*i
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语言中如何实现。
非常感谢
avatar
l*8
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);
avatar
t*i
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);

avatar
t*i
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);

avatar
l*8
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
:

avatar
k*f
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
:

avatar
t*i
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.

avatar
f*y
8
按新标准可以

【在 t*i 的大作中提到】
: C++能在代码的任意地方定义变量。似乎C不可以,只能定义在开头。原先也犯了这个错
: 误。
:
: is
: locations.

avatar
s*n
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);

avatar
F*g
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);

avatar
A*g
13
读到逗号的符号是%[^','],
比如
sscanf(str,"%[^','],%[^','] ...",para1,para2);
个人不喜欢parse逗号,我宁愿把逗号换成空格,比如
avatar
F*g
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.
avatar
F*g
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.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。