LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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, 08:50 PM   #1
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 84
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, 08:57 PM   #2
paulsm4
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, 03: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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 12:43 PM
C - How to initialize an undetermined number of variables golmschenk Programming 11 02-07-2010 12:25 PM
C Syntax - Passing in unknown number of arguments? CoderMan Programming 2 05-18-2009 05:41 AM
ftp: connect :Unknown error number chobo Linux - Newbie 10 07-17-2008 07:09 AM
Using sscanf to grab an unknown number of variables R00ts Programming 4 06-06-2005 06:03 PM


All times are GMT -5. The time now is 03:54 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration