Read file into array

This is a discussion on Read file into array within the C Programming forums, part of the General Programming Boards category; Hey! I've a text file (database.db) looking like this: Code: firstname1 lastname1 town1 country1 number1 firstname2 lastname2 town2 country2 number2 ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    3

    Read file into array

    Hey!

    I've a text file (database.db) looking like this:

    Code:
    firstname1 lastname1 town1 country1 number1
    firstname2 lastname2 town2 country2 number2
    firstname3 lastname3 town3 country3 number3
    firstname4 lastname4 town4 country4 number4
    ...
    And I want to read each column in each row into a two dimensional array, so the result looks like this:

    Code:
    array {
     array {firstname1, lastname1, town1, country1, number1}, 
     array {firstname2, lastname2, town2, country2, number2}, 
     array {firstname3, lastname3, town3, country3, number3}, 
     array {firstname4, lastname4, town4, country4, number4},
     ...
    }
    How can I do this in C?

    Thanks!

    With regards,

    Simon

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What have you tried?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    I've just made it this far:

    Code:
    char buffer[1024];	
    db = fopen("database.db", "r");
    
    while(fgets(buffer, 1024, db) != NULL)
    {
       // Stuck here
    }
    I don't know how to go through each white space seperated string... :/ I tried to use fscanf inside the loop, but fscanf requires a pointer to a file descriptor (?) so I didnt get any further...

    S

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could certainly use fscanf() - it uses the same pointer type as fgets(), so if db is OK for fgets() then it should work for fscanf() too.

    Another option is to read the entire string and then use strtok() or sscanf() to split the string into separate data items.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    3
    sscanf seems to do the trick thanks! However, why doesnt this works? I get Segmentation fault.

    PHP Code:
    char *str "this is a test";
    char array[1][4];
    sscanf(str"%s %s %s %s", array[0][0], array[0][1], array[0][2], array[0][3]); 
    It works if I read it into variables but arrays wont work...

    S

  6. #6
    Registered User bboozzoo's Avatar
    Join Date
    Jan 2009
    Posts
    14
    not really array[1][4] is an array of char[4] with only 1 element
    why not use something like this:
    Code:
    #define MAX_FIELD_LEN 40
    struct tuple_s {
      char field1[MAX_FIELD_LEN];
      char field2[MAX_FIELD_LEN];
      char field3[MAX_FIELD_LEN];
      char field4[MAX_FIELD_LEN];
    };
    /* and then */
    struct tuple_s * el = some_pointer_to_type_tuple_s;
    sscanf(str, "%s %s %s %s", el->field1, el->field2, el->field3, el->field4);
    Quote Originally Posted by neobarn View Post
    sscanf seems to do the trick thanks! However, why doesnt this works? I get Segmentation fault.

    PHP Code:
    char *str "this is a test";
    char array[1][4];
    sscanf(str"%s %s %s %s", array[0][0], array[0][1], array[0][2], array[0][3]); 
    It works if I read it into variables but arrays wont work...

    S

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by neobarn View Post
    sscanf seems to do the trick thanks! However, why doesnt this works? I get Segmentation fault.

    PHP Code:
    char *str "this is a test";
    char array[1][4];
    sscanf(str"%s %s %s %s", array[0][0], array[0][1], array[0][2], array[0][3]); 
    It works if I read it into variables but arrays wont work...

    S
    You need to read a string into an array of characters -- you are trying to read a string into a char (array[0][0] is just a char -- array[0] is an array of chars). Probably you mean to define array as
    Code:
    char array[1][4][WORD_LEN];
    where in your example WORD_LEN has to be at least 5.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Read from file to an array
    By brooklyn1126 in forum C++ Programming
    Replies: 2
    Last Post: 05-15-2004, 09:32 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21