Ignore 'E' when reading double with sscanf

后端 未结 3 811
甜味超标
甜味超标 2020-12-17 16:11

I have input such as \"(50.1003781N, 14.3925125E)\" .These are latitude and longitude.

I want to parse this with

sscanf(string,\"(%lf%c         


        
3条回答
  •  一整个雨季
    2020-12-17 16:33

    Process the string first using

    char *p;
    while((p = strchr(string, 'E')) != NULL) *p = 'W';
    while((p = strchr(string, 'e')) != NULL) *p = 'W';
    
    // scan it using your approach
    
    sscanf(string,"(%lf%c, %lf%c)",&a,&b,&c,&d);
    
    // get back the original characters (converted to uppercase).
    
    if (b == 'W') b = 'E';    
    if (d == 'W') d = 'E';
    

    strchr() is declared in the C header .

    Note: This is really a C approach, not a C++ approach. But, by using sscanf() you are really using a C approach.

提交回复
热议问题