LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-29-2010, 07:50 PM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy .. bullseye bookworm
Posts: 1,834

Rep: Reputation: 108Reputation: 108
C: sscanf but number of variables unknown


Hya,

Goal:
assign integers into array form string.

From string
"123,456,789,0123,4567,8901" (comma separated 6 integer numbers)
To array
a[0] = 123; a[1] = 456 ....a[5] = 8901

Idea is sscanf(string,"%d%d%d...",&(a[0]),&(a[1]),&(a[2])...)

Problem is that array size is unknown until the string is available for scanning. It could be huge.

What I thought:
Sounds like file scanning, it is easy. But, scanning same string gives me same value.

Umm.. vscanf or something else would do, once format string is determined. Yes, the format string can be made without any trouble. but how to pass variable??

What I did:
scan one variable, then manipulate string to clear fields from top to comma, scan next variable. (loop)

Assessment:
My way works, however, is there any smarter way?

Happy Penguins!
 
Old 11-29-2010, 07:57 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Personally, I'd vote "for loop".

Or maybe implement your own list or vector struct (since it sounds like you're programming in C, and can't use an STL container).

But if you want a function that takes a variable number of arguments (and, of course, if you know the #/arguments before you call the function), then "stdarg" is what you're looking for.
 
Old 11-30-2010, 02:52 AM   #3
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
The canonical way would be to use strtok() and then atoi().
 
  


Reply

Tags
ansi-c, scan, string



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Counting the number of exit variables - Bash. 0bfuscated Programming 3 07-02-2010 11:43 AM
C - How to initialize an undetermined number of variables golmschenk Programming 11 02-07-2010 11:25 AM
C Syntax - Passing in unknown number of arguments? CoderMan Programming 2 05-18-2009 04:41 AM
ftp: connect :Unknown error number chobo Linux - Newbie 10 07-17-2008 06:09 AM
Using sscanf to grab an unknown number of variables R00ts Programming 4 06-06-2005 05:03 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration