由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一个c语言的问题
相关主题
c++ 中如何把str转换为float?difference between: char** p and char*p[] ??
const char *p, is it ok to change p[1] ?why int** cannot convert to const int** ?
array和pointer在作为函数返回时有啥区别 (C)what's wrong with this C++ code?
问sscanfA question about c++ pointer
Array in Ca simple C++ question
sscanf problem in MSVC 7请问const myClass &src 和myClass const &src有什么区别?
dereference a NULL pointer in C能帮忙看看这个字符串的操作问题吗?
new了指针,delete的时候出错了pointer to function
相关话题的讨论汇总
话题: s3话题: gps话题: sscanf话题: data话题: s%
进入Programming版参与讨论
1 (共1页)
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
10
http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml

【在 s****n 的大作中提到】
: 有人能稍微解释一下么?
:
: s3

相关主题
sscanf problem in MSVC 7difference between: char** p and char*p[] ??
dereference a NULL pointer in Cwhy int** cannot convert to const int** ?
new了指针,delete的时候出错了what's wrong with this C++ code?
进入Programming版参与讨论
s****n
发帖数: 700
11
读取s3的那部分看不明白阿, 那么多\["的符号

【在 H***a 的大作中提到】
: http://docs.roxen.com/pike/7.0/tutorial/strings/sscanf.xml
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.
1 (共1页)
进入Programming版参与讨论
相关主题
pointer to functionArray in C
[合集] 这样写有什么不好?sscanf problem in MSVC 7
Pointer to iterator?dereference a NULL pointer in C
问一道brainbench上的问题new了指针,delete的时候出错了
c++ 中如何把str转换为float?difference between: char** p and char*p[] ??
const char *p, is it ok to change p[1] ?why int** cannot convert to const int** ?
array和pointer在作为函数返回时有啥区别 (C)what's wrong with this C++ code?
问sscanfA question about c++ pointer
相关话题的讨论汇总
话题: s3话题: gps话题: sscanf话题: data话题: s%