sscanf() for just one field
-
9ine wrote:
sscanf(str,"%s %s %s",0,0,tmp);
Replace zeroes by address of variables because sscanf expects some address to be passed in. You are passing in NULL hence the exception I guess.
char str[] = "15 12 14";
int num1,num2,num3;
num1=num2=num3=0;sscanf(str, "%d %d %d", &num1, &num2, &num3 );
printf( "Num1: %d, num2: %d, num3: %d", num1, num2, num3 );
Nibu thomas A Developer Programming tips[^] My site[^]
-
How to read from a string char str[] = "field1 field2 field3 ..." only one field3 string using sscanf() and one buffer for it - tmp. sscanf(str,"%s %s %s",0,0,tmp); it produces exception in this case
9ine
9ine wrote:
sscanf(str,"%s %s %s",0,0,tmp);
Use:
sscanf(str, "%*s %*s %s", tmp);
9ine wrote:
it produces exception in this case
As it should since you are trying to write to address 0.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
9ine wrote:
sscanf(str,"%s %s %s",0,0,tmp);
Use:
sscanf(str, "%*s %*s %s", tmp);
9ine wrote:
it produces exception in this case
As it should since you are trying to write to address 0.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
I want to use just one variable to its arguments list. Consider if I need to read 20th field from a string I dont want to declare int num1,num2,num3,num4, ........... num20 to read last field :-) 9ine
9ine wrote:
Consider if I need to read 20th field from a string I dont want to declare int num1,num2,num3,num4, ........... num20 to read last field :-)
sscanf isn't the right function for doing this. And using the %s option for sscanf isn't advised since it will almost never do what you hope it will. In fact, only your first %s will be read in and you will corrupt memory with the other 2 (even after fixing your references to the variables). If you are looking to access a single (or even a few) elements in a delimited string, write a split method, or use strtok to march through the array to the elements you want to access.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
9ine wrote:
sscanf(str,"%s %s %s",0,0,tmp);
Use:
sscanf(str, "%*s %*s %s", tmp);
9ine wrote:
it produces exception in this case
As it should since you are trying to write to address 0.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
How to read from a string char str[] = "field1 field2 field3 ..." only one field3 string using sscanf() and one buffer for it - tmp. sscanf(str,"%s %s %s",0,0,tmp); it produces exception in this case
9ine
Or like this. N.B. Add error handling as required!
char str[] = "field1 field2 field3 ..." // only one field3 string using sscanf() and one buffer for it - tmp. char * cpLastSpace = strrchr(str, (int) ' '); // Find the last space in str if(NULL != cpLastSpace) { sscanf(cpLastSpace+1,"%s",tmp); // Or strcpy(tmp, cpLastSpace+1); }
Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson
-
I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
Sebastian Schneider wrote:
I am gonna remove all stringstreams from my code, right now
are you serious :~ C++ classes are much prefered than C functions dude!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
-
9ine wrote:
sscanf(str,"%s %s %s",0,0,tmp);
Use:
sscanf(str, "%*s %*s %s", tmp);
9ine wrote:
it produces exception in this case
As it should since you are trying to write to address 0.
"Money talks. When my money starts to talk, I get a bill to shut it up." - Frank
"Judge not by the eye but by the heart." - Native American Proverb
-
I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
Sebastian Schneider wrote:
Wow. I am gonna remove all stringstreams from my code, right now
I do hope you were being sarcastic :-P sscanf is great when parsing typed information ... it is not very useful for trying to extract substrings.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
I did not know sscanf could do that. Wow. I am gonna remove all stringstreams from my code, right now ;)
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
Sebastian Schneider wrote:
I am gonna remove all stringstreams from my code, right now
are you serious :~ C++ classes are much prefered than C functions dude!
TOXCCT >>> GEII power
[VisualCalc 3.0 updated ][Flags Beginner's Guide new! ]
toxcct wrote:
are you serious
Hrhr.
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
Sebastian Schneider wrote:
Wow. I am gonna remove all stringstreams from my code, right now
I do hope you were being sarcastic :-P sscanf is great when parsing typed information ... it is not very useful for trying to extract substrings.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
I was. Oh, and I noticed 2 typos in your Signature:
Zac Howland wrote:
...for 8 hours a day, 5 days a week
should be
experience tells us:
...for 11 hours a day, 6 days a week
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
I was. Oh, and I noticed 2 typos in your Signature:
Zac Howland wrote:
...for 8 hours a day, 5 days a week
should be
experience tells us:
...for 11 hours a day, 6 days a week
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
Just checking ... And I've thought about changing it to say "at least 8 hours ... 5 days" since my old job fit the 20 hour/7 day mold.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
-
Just checking ... And I've thought about changing it to say "at least 8 hours ... 5 days" since my old job fit the 20 hour/7 day mold.
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac
Gotta loooooove crunch-time...
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
-
Gotta loooooove crunch-time...
Cheers, Sebastian -- Contra vim mortem non est medicamen in hortem.
Oh no. That was normal operating procedure ... When crunch time came around, the managers would pressure us to spend the night in the office ... even over weekends!
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac