0

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.

1
  • Where you reading from? File or stdin? Commented Dec 29, 2010 at 1:40

2 Answers 2

5

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" 
Sign up to request clarification or add additional context in comments.

Comments

2

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 :).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.