If I have a input string like
rows: 3
I'd like to assign that "rows" to a particular string and "3" to a integer variable. how do I do it...
similar to python where I simply take
Code:val = int ( raw_input("Enter") )
If I have a input string like
rows: 3
I'd like to assign that "rows" to a particular string and "3" to a integer variable. how do I do it...
similar to python where I simply take
Code:val = int ( raw_input("Enter") )
I would think that you have seen how scanf, or fgets with sscanf or strtol or atoi, have been used.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thanks.. I forgot.. I could use strtok()..
So, is it now your plan to run around following me from thread to thread telling people that simple solutions --that I test before posting-- don't work?
I'm betting you didn't even read the original message where he said he wanted to put the first part in a string and the second part in an int... you just decided to try to tell him that a perfectly simple, by the book, solution wouldn't work and then substitute your own.
I have not test your wrong solution, i have see it.
A simple wrong solution is a wrong solution and if you have problems to post simple working C code, you should never post your wrong newbie mistakes here.
Well I went and tested both, and for some reason CommonTater's example does only gets the string, but BillyTKid's example only gets the int. In fact for some reason I couldn't get it to read in both using a single scanf. Probably something to do with me mostly using C++ rather than C. If anyone can explain exactly why CommonTater's example didn't read in the integer, or how to make it work inside a single scanf then please share.
FWIW, Here's a solution that works, complete with buffer overrun protection:Code:char input[] = "rows: 3"; char str[20]; int num; if (sscanf(input," %20s*[^:]", str) + sscanf(input,"%*[^:]:%d", &num) == 2) { //Bingo! }
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Looking at the op's sample again you may need...
It's pattern matching so sometimes you have to mess with things a little....Code:sscanf(string," %s: %d",string,&num);
yes, CommonTater's 1st threat solution is not working...
But guys I'm just not looking for string like this rows:2 but after rowsif can be any non-numeric char that has to be eliminated...
I tried like this.. here str is taking taking the whole part of "string" and num is showing 0...Code:sscanf(string,"%s %*c %d",str, &num);
> It's pattern matching so sometimes you have to mess with things a little....
You should really listen to BillyTKid sometime and not go round biting everyone's head off every time they correct one of your mistakes.
%s will also eat the :, which prevents the rest of the format string matching at all when it tries to match a literal : as well.Code:$ cat bar.c #include <stdio.h> int main() { char input[] = "rows: 3"; char string[100] = { 0 }; int num = 0; int result = sscanf(input," %s: %d",string,&num); printf("%d %s %d\n", result, string, num); return 0; } $ gcc bar.c $ ./a.out 1 rows: 0
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Haven't tested, but I don't think that is correct because order of evaluation is unspecified.Originally Posted by iMalc
Last edited by laserlight; 11-06-2011 at 12:45 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
Oh yeah. You're using sscanf, not scanf, so that is fine. If you used scanf instead, then the order matters because the input buffer would be changed.Originally Posted by iMalc
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)