View Full Version : Best way to do this?

October 23rd, 2009, 03:53 PM
I'm thinking of a program that gets passed a file of data, eg:

Name Score
Bob 2
Mat 3
Jane 7

then makes a table like this:

|Name |Score |
|Bob |2 |
|Mat |3 |
|Jane |7 |

the number of fields may be more than two.
My idea was:

get 1st line
scanf("%s", string);
int a = spaces_count(string);
if (a == 2){
scanf("%s %s", num[0], num[1]);

Is there a better way?

October 23rd, 2009, 04:23 PM
your solution will not work for arbitrary field numbers
better read the whole line and tokenize it using the field delimiter (see strtok)
or read single characters and check for the field delimiter to split fields

October 23rd, 2009, 06:39 PM
If you mean a "table" as in a relational-database table, you may consider parsing it with the database engine.

In MySQL: http://dev.mysql.com/doc/refman/5.0/en/mysqlimport.html

And if you still want to do it via an ad-hoc method, I would suggest using a scripting language.