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! |
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. |
The canonical way would be to use strtok() and then atoi().
|
All times are GMT -5. The time now is 04:19 PM. |