-
Best way to do this?
I'm thinking of a program that gets passed a file of data, eg:
Code:
Name Score
Bob 2
Mat 3
Jane 7
then makes a table like this:
Code:
|------------|
|Name |Score |
|-----|------|
|Bob |2 |
|Mat |3 |
|Jane |7 |
|------------|
the number of fields may be more than two.
My idea was:
Code:
get 1st line
scanf("%s", string);
int a = spaces_count(string);
a++;
if (a == 2){
scanf("%s %s", num[0], num[1]);
}
Is there a better way?
-
Re: Best way to do this?
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
-
Re: Best way to do this?
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.