i want to read a string containing integers and store all integers in some integer variables. e.g
str[]="12,23,45"
can anyone please help me out. i tried for a space separated integers..
#include<string.h>
#include<stdio.h>
int main()
开发者_如何转开发 {
char buffer[]="12,34,56";
int x,y,z;
if(sscanf(buffer,"%d%d%d",&x,&y,&z)>2);
{
printf("%d\n",x);
printf("%d\n",y);
printf("%d\n",z);
}
return 0;
}
Thanks for your precious time. stay happy.
You are almost there!
sscanf()
requires you to give the format of the expected string to be parsed.
You have commas between numbers... Just try to add commas between the %d
...
"%d,%d,%d"
Option 1: use a combination of standard library functions such as strtok()
and atoi()
, or sscanf()
. With these tools, you'll figure it out in no time!
Option 2: roll out your own function! It's a nice thinking excerise :).
精彩评论