Results 1 to 3 of 3

Thread: Best way to do this?

  1. #1
    Join Date
    Dec 2008
    Location
    UK
    Beans
    438
    Distro
    Ubuntu 10.04 Lucid Lynx

    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?

  2. #2
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    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

  3. #3
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    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.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •