Drugo
Junior Member level 2
- Joined
- Feb 23, 2011
- Messages
- 20
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Activity points
- 1,537
Code C - [expand] 1 2 3 4 5 6 7 8 // 99 init values just for debugging resons char string[] = "test1 12,1.525,2.789"; char theChar1 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%hhd",&theFloat1, &theFloat2, &theChar1);
Code C - [expand] 1 2 3 4 5 6 7 8 9 // 99 init values just for debugging resons char string[] = "test1 12,1.525,2.789"; char theChar1 = 99; char theChar2 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1, &theChar2);
Code C - [expand] 1 2 3 4 5 6 7 8 9 // 99 init values just for debugging resons char string[] = "test1 12,1.525,2.789"; char theChar1 = 99; char theChar2 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1, &theChar2, &theChar3);
input: 12,1.525,2.789
match: (%f->12)(,->,)(%f->1.525)(,->,)(%hhd->2) (since the . is not a valid digit for an integer)
input: 12,1.525,2.789
match: (%f->12)(,->,)(%f->1.525)(,->,)(%hhd->2) (note that your return value is three and not four, as the sscanf parser is stuck on the period and you are telling it to match a comma)
input: 12,1.525,2.789
match: (%f->12)(,->,)(%f->1.525)(,->,)(%hhd->2) (still stuck on the period...)
I'm using a Microchip C30 compiler on dsPIC33F family and struggling A LOT with sscanf() function. Please please please help me because my project is getting stuck for this little detail. I really hope that someone can help me, I haven't found anything on internet and I spent the last 2 days on this issue. Thanks a lot in advance.
-----------------------
IMPORTANT NOTE (10-Aug, 12:30pm PST): in my original post the "char string[] = ..." instructions in ALL three examples I reported (A, B and C below) were wrong due to a copy and paste mistake with code examples I took from my tests. I corrected these three examples as you can see below (see also the comment). Sorry for that!
-----------------------
THE TASK
I have to read different types of numbers from a string (unsigned int, float, char and unsigned char).
THE ISSUES
I have problems reading numeric char and unsigned char (i.e. 8 bit numbers, not ASCII characters), but not the other ones. With problem I mean that sscanf() fails (crashes) during its execution without returning any numeric value. In particular the problems come when I try to read more than one char (or unsigned char) in a row. As a char conversion specifier I use %hhd, but I tried - at least I think - with all the possible combinations. For unsigned char I used %hhu.
In order to understand better please look at this examples with just float and char. Float are read correctly, char just in case A) with one char parameter.
A) JUST ONE CHAR HAS TO BE READ: it works correctly :grin: :lol:
// 99 init values just for debugging resons
//char string[] = "test1 12,1.525,2.789"; -> WRONG INSTRUCTION I WROTE IN MY FIRST POST! PLEASE DON'T CONSIDER IT
char string[32] = "test1 1.525,2.789,12"; // THIS IS THE RIGHT ONE!
char theChar1 = 99;
float theFloat1;
float theFloat2;
int retVal = 99;
retVal = sscanf(&string[6],"%f,%f,%hhd",&theFloat1, &theFloat2, &theChar1);
B) TWO CHARS HAVE TO BE READ: it doesn't work, sscanf() does not complete its execution and doesn't provide a return value :-(
// 99 init values just for debugging resons
//char string[] = "test1 12,1.525,2.789"; -> WRONG INSTRUCTION I WROTE IN MY FIRST POST! PLEASE DON'T CONSIDER IT
char string[32] = "test1 1.525,2.789,12,13"; // THIS IS THE RIGHT ONE!
char theChar1 = 99;
char theChar2 = 99;
float theFloat1;
float theFloat2;
int retVal = 99;
retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1, &theChar2);
C) THREE CHARS HAVE TO BE READ: it doesn't work, sscanf() does not complete its execution and doesn't provide a return value :-x
// 99 init values just for debugging resons
//char string[] = "test1 12,1.525,2.789"; -> WRONG INSTRUCTION I WROTE IN MY FIRST POST! PLEASE DON'T CONSIDER IT
char string[32] = "test1 1.525,2.789,12,13,14"; // THIS IS THE RIGHT ONE!
char theChar1 = 99;
char theChar2 = 99;
char theChar3 = 99;
float theFloat1;
float theFloat2;
int retVal = 99;
retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1, &theChar2, &theChar3);
THANKS SO MUCH FOR YOUR HELP!!!
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 // 99 init values just for debugging resons char string[32] = "test1 1.525,2.789,12,13,14"; char theChar1 = 99; char theChar2 = 99; char theChar3 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1); // IN THIS INSTRUCTION ALL OK! retVal = sscanf(&string[6],"%hhd", &theChar2); // IT CRASHES retVal = sscanf(&string[21],"%hhd", &theChar3); // NOT EXECUTED
retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1); // IN THIS INSTRUCTION ALL OK!
retVal = sscanf(&string[6],"%f,%f,%hhd",&theFloat1, &theFloat2, &theChar1); // IN THIS INSTRUCTION ALL OK!
retVal = sscanf(&string[6],"%f,%f,%hhd,%hhd,%hhd",&theFloat1, &theFloat2, &theChar1, &theChar2, &theChar3); // IN THIS INSTRUCTION ALL OK!
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 // 99 init values just for debugging resons char string[32] = "test1 1.525,2.789,12,13,14"; char theChar1 = 99; char theChar2 = 99; char theChar3 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%hhd",&theFloat1, &theFloat2, &theChar1); // IN THIS INSTRUCTION ALL OK! retVal = sscanf(&string[6],"%hhd", &theChar2); // IT CRASHES retVal = sscanf(&string[21],"%hhd", &theChar3); // NOT EXECUTED
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 char string[32] = "test1 1.525,2.789,12,13,14"; char theChar1 = 99; char theChar2 = 99; char theChar3 = 99; int tempInt1 = 99; int tempInt2 = 99; int tempInt3 = 99; float theFloat1; float theFloat2; int retVal = 99; retVal = sscanf(&string[6],"%f,%f,%d,%d,%d",&theFloat1, &theFloat2, &int tempInt1, &int tempInt2, &int tempInt3); // ALL OK HERE! theChar1 = (char) tempInt1; theChar2 = (char) tempInt2; theChar3 = (char) tempInt3;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NUM_VALUES 20 #define TYPE_FLOAT 1 #define TYPE_BYTE 2 union value_contents { float flt; char byte; }; struct extracted_value { char type; union value_contents value; }; static void parseMyString( char *string, int *numValues, struct extracted_value *values ) { /* note that strtok will blast your original string, so if you care about it, make a copy of it here and pass that to strtok. otherwise, blast away... */ char *part = strtok(string, " "); /* if this didn't hit, we don't have a space in your string, so the input isn't following our assumed format */ if ( !part ) return; while ( (part = strtok(NULL, ",")) ) { /* see if we've run out of room for our output */ if ( *numValues >= MAX_NUM_VALUES ) break; /* okay, we've got a nul terminated string (part) that we can play with. First let's figure out if it is a float... */ if ( strchr(part, '.') ) { values[*numValues].type = TYPE_FLOAT; values[(*numValues)++].value.flt = atof(part); } else { values[*numValues].type = TYPE_BYTE; values[(*numValues)++].value.byte = atoi(part); } } } static void printMyStuff( int numValues, const struct extracted_value *values ) { int i; int numBytes = 0; int numFloats = 0; for ( i = 0; i < numValues; ++i ) switch( values[i].type ) { case TYPE_FLOAT: ++numFloats; printf("Extracted value #%d is a float = %0.3f\n", i+1, values[i].value.flt); break; case TYPE_BYTE: ++numBytes; printf("Extracted value #%d is a byte = %d\n", i+1, values[i].value.byte); break; } printf("Extracted %d byte%s.\n", numBytes, ((numBytes == 1) ? "" : "s")); printf("Extracted %d float%s.\n", numFloats, ((numFloats == 1) ? "" : "s")); } int main(int argc, char **argv) { char string[32] = "test1 1.525,2.789,12,13,14"; int numValues = 0; struct extracted_value values[MAX_NUM_VALUES]; printf("Original input string: %s\n", string); parseMyString(string, &numValues, values); printMyStuff(numValues, values); return 0; }
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?