Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何从char[]字符串中提取数据_C++_Character Encoding_Char_Arduino - Fatal编程技术网

C++ 如何从char[]字符串中提取数据

C++ 如何从char[]字符串中提取数据,c++,character-encoding,char,arduino,C++,Character Encoding,Char,Arduino,目前,我有一个GPS连接到我的Arduino芯片,该芯片每秒输出几行。我想从某些行中提取特定信息 $ÐÐÁ,175341.4583355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,0000*57 (注意字符) 如果我将此行读入char[],是否可以从中提取3355.7870和01852.4251?(很明显是这样,但如何做到?) 我需要数一数逗号,然后在逗号2之后开始把数字放在一起,在逗号3处停止,然后对第二个数字做同样的操作,还是有其他方法?一种分割

目前,我有一个GPS连接到我的Arduino芯片,该芯片每秒输出几行。我想从某些行中提取特定信息

$ÐÐÁ,175341.4583355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,0000*57

(注意字符)

如果我将此行读入
char[]
,是否可以从中提取
3355.7870
01852.4251
?(很明显是这样,但如何做到?)

我需要数一数逗号,然后在逗号2之后开始把数字放在一起,在逗号3处停止,然后对第二个数字做同样的操作,还是有其他方法?一种分割阵列的方法

另一个问题是识别这一行是因为它的开头有奇怪的字符-我如何检查它们,因为它们不正常并且行为奇怪

我想要的数据总是以
xxxx.xxxx
yyyyy.yyyy
的形式存在,并且在该形式中是唯一的,这意味着我可以通过所有数据进行搜索,而不关心它在哪一行,并提取该数据。几乎像preg匹配,但我不知道如何使用
char[]
来实现这一点

任何提示或想法?

您可以使用标记(拆分)逗号上的字符串,然后使用解析数字

编辑:C示例:

void main() {
    char * input = "$ÇÐÇÇÁ,175341.458,3355.7870,Ó,01852.4251,Å,1,03,5.5,-32.8,Í,32.8,Í,,0000*57";

    char * garbage = strtok(input, ",");
    char * firstNumber = strtok(NULL, ",");
    char * secondNumber = strtok(NULL, ",");
    double firstDouble;
    sscanf(firstNumber, "%lf", &firstDouble);
    printf("%f\n", firstDouble);
}

如果字符串开头有奇怪的字符,则应从结尾开始解析:

char* input = get_input_from_gps();
// lets assume you dont need any error checking
int comma_pos = input.strrchr(',');
char* token_to_the_right = input + comma_pos;
input[comma_pos] = '\0';
// next strrchr will check from the end of the part to the left of extracted token
// next token will be delimited by \0, so you can safely run sscanf on it 
// to extract actual number

注意
strtok
在适当的位置更改字符串。这必须是
char input[]=。您的代码尝试修改字符串文字,给出未定义的行为。(当然,这在真正的代码中不是问题)。