techie said:I have the following variables
int number;
char text[100];
I have a text string as follows
CMGL: 9,"REC READ","0137443256780",,149,8
I want to read the text
number = 9
text = 0137443256780 without the "" quotes
Will the following work,
sscanf(string,"CMGL:%u,\"%s\",,",&number,text);
or any other suggestions.
#include <stdio.h>
int main(void)
{
int number;
char text[100];
char *string = "CMGL: 9,\"REC READ\",\"0137443256780\",,149,8";
printf("%s\n", string);
if (2 == sscanf(string, "CMGL:%u,\"%s\",,", &number, text))
printf("number=%d text=%s\n", number, text);
else
printf("Trouble!\n");
return 0;
}
CMGL: 9,"REC READ","0137443256780",,149,8
number=9 text=REC
#include <stdio.h>
int main(void)
{
int number;
char text[100];
char *string = "CMGL: 9,\"REC READ\",\"0137443256780\",,149,8";
printf("%s\n", string);
if (2 == sscanf(string, "CMGL:%d,\"REC READ\",\"%99[0-9]s", &number, text))
printf("number=%d text=%s\n", number, text);
else
printf("Trouble!\n");
return 0;
}
CMGL: 9,"REC READ","0137443256780",,149,8
number=9 text=0137443256780
char *string = "CMGL: 9,\"Anything 1234\",\"0137443256780\",,149,8";
sscanf(string, "CMGL:%d,,\"%99[0-9]s", &number, text); // skip the middle string
#include <stdio.h>
int main(void)
{
int number;
char text[100];
char *string = "CMGL: 9,\"Anything 1234\",\"0137443256780\",,149,8";
printf("%s\n", string);
if (2 == sscanf(string, "CMGL:%d,\"%*[^\"]\",\"%99[0-9]", &number, text))
printf("number=%d text=%s\n", number, text);
else
printf("Trouble!\n");
return 0;
}
CMGL: 9,"Anything 1234","0137443256780",,149,8
number=9 text=0137443256780
"\2- 12.345Kg ST\r\n"
#include <ctype.h>
#include <stdio.h>
unsigned char strings[][20] =
{
{"\2- 234.45Kg ST\r\n"},
{"\2 1234.45Kg ST\r\n"},
{"\2 234.45 Lbs\r"},
{"\2234.5 Kg\r\n"},
{"\2- 234.45Kg US\r\n"},
{"\2 12345Kg T\r\n"}
};
double give_me_number(unsigned char *str_p)
{
double digit;
char dot_flag;
unsigned char count;
// scan string for '2' character
while(*str_p++ != '\2')
;
// Scan string for first digit
while(!isdigit(*str_p))
str_p++;
// Convert digit to number starting from found in one in previous loop
for(digit = 0, dot_flag = 0, count = 0 ; ; str_p++)
{
if(isdigit(*str_p))
{
digit = digit*10 + *str_p - 48;
if(dot_flag == 1)
count++;
}
else if(*str_p == '.')
dot_flag = 1;
else
break;
}
while(count-- !=0)
digit /= 10.0;
return digit;
}
void main(void)
{
unsigned char temp;
for(temp = 0 ; temp < 6; temp++)
{
printf("\n\nnumber from string %s \n\t is %lf \n", strings[temp], give_me_number(strings[temp]));
}
}
/*******************************************************************
Simple Comma Separated String Parser.
Input String = something like "START: 123,"ABCD",EFGH,345,1,,9\r\0"
1. Matches the Start HEADER with strtstr (matches "START:")
2. Counts Commas upto the required parameter (0 is the first param)
3. Copies the parameter text till comma, ", \r or \0 to desination string
4. Removes the starting and ending quotes "" if any.
5. Returns 1 if successfull, 0 if Unsucessfull
Use like : result = csv_str_parse(string,"+CMGL:",i,outstring);
*******************************************************************/
int csv_str_parse(char * instr, char * strtstr, int para_no, char * outstr) {
int index;
int j;
char cc;
index = strlen(strtstr);
if (strncmp(strtstr,instr,index) != 0) return 0; // start does not match
while (para_no != 0) { // start of the required parameter
cc = instr[index++];
if (cc == '\0') return(0);
if (cc == ',') para_no--;
}
if (instr[index] == '\"') index++; // ignore the start quote "
j = 0;
cc = instr[index++];
while (cc!=',' && cc!='\r' && cc!= '\"' && cc!='\0') {
outstr[j++] = cc;
cc = instr[index++];
}
outstr[j] = '\0'; // add a terminator
return (1);
}
My favorite C book is the standard itself, although it is not a tutorial. The most recent version I've seen is ISO/IEC 9899 second edition dated 1999-12-01. Someone uploaded this copy - grab it quickly because the link will disappear soon!Can you post some file or tell me some website which describes the scanf in details.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?