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") )
Printable View
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.
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!
}
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
Haven't tested, but I don't think that is correct because order of evaluation is unspecified.Quote:
Originally Posted by iMalc
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.Quote:
Originally Posted by iMalc
Well when you post crappy answers, expect to get called on it - that's all.
But you seem to take any such critisism far too personally IMO.
OK, show me exactly where BillyTKid said "You don't know what you're talking about"
Post #5 was a simple correction, and made NO STATEMENT at all as to your competence (or lack of).
Then you boom back with Ad hominem - Wikipedia, the free encyclopedia in post #6. You didn't argue with the post, you were just ........ed that someone posted something better - and that is all there is to it. You were wrong, now man up and accept the critisism.
You took the gloves off first, so when Billy posted #7 you really lost the plot.
Because not so long ago, you got your panties in such a bunch when people were trying to put words in your mouth (even in jest).
http://cboard.cprogramming.com/c-pro...ml#post1067212 post #26 and it's not the first time.
I'm getting very close to telling you what I told laserlight the other day.Quote:
Post #5 was a simple correction, and made NO STATEMENT at all as to your competence (or lack of).
Then you boom back with Ad hominem - Wikipedia, the free encyclopedia in post #6. You didn't argue with the post, you were just ........ed that someone posted something better - and that is all there is to it. You were wrong, now man up and accept the critisism.
WRONG... look again!Quote:
You took the gloves off first, so when Billy posted #7 you really lost the plot.
Yes, well, just about the most plainly despicable thing a person can do on a forum like this is editing quotations and then attributing them back to the OP even in jest ... you have no idea the misunderstanding and damage that can cause a person.Quote:
Because not so long ago, you got your panties in such a bunch when people were trying to put words in your mouth (even in jest).
Yes it infuriates me that someone would be so cavalier with something so easily misunderstood.
Right, so now you take random snippets from any random thread and post them WITHOUT cross-referencing.
I simply don't have the time to search through every one of your posts just to see if what you're saying is true.
Right now, I'm thinking this about you.
Ethics prevents me from posting the insults, deprecation and threats you sent me privately in response to an attempt at explaination. However; your cute little link says more than enough to make my point.
There is something VERY WRONG with a forum where moderators go around belittling and threatening members.