Reputation: 857
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.
Upvotes: 0
Views: 1216
Reputation: 28850
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"
Upvotes: 5
Reputation: 77029
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 :).
Upvotes: 2