Thread: Read file into array

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    8

    Read file into array

    I'm trying to read a file into an array. The data (.txt) is in a large table (200X10) and contains both text and integers. I'm just coming back to C after using matlab for a long time, so I'm really just looking for a link to a good refresher on how I can simply import and organize the data using C. Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The "import" of the data would best be done by fgets() to read a line, and then suitable line-parsing. Depending on the format you have two choices:
    1. manually separate out the portions of the string, using for example strtok(). Once you have the component of a string, you would use strcpy() to copy string data and the strtod(), strtol() functions to convert to double and long respectively/
    2. use sscanf() to read the string.

    Which one of the above you use would depend on the exact formatting of the string. If you could post a few "typical" lines of your data it would help.

    --
    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
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Depends on the format of the file and exactly how much you have to read. If it's line-based entries of numbers and strings, you could create a struct for each entry and a function to read such a line from a FILE *.

  4. #4
    Lost in the C ZaC's Avatar
    Join Date
    Jun 2008
    Location
    Italy
    Posts
    47
    I think you could like the example in this reference to fread() function: http://www.cplusplus.com/reference/c...dio/fread.html

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ZaC View Post
    I think you could like the example in this reference to fread() function: http://www.cplusplus.com/reference/c...dio/fread.html
    Although fread() tends to be tricky to use with text-input. It would possibly work out OK if every line is the same length. But the benefit over fgets() is small - for large files, the majority of the time consumed would be idle-time waiting for the disk to deliver the file-content. On a small file (and 200 lines is definitely small in this circumstance), I would expect fgets() to read the whole file in less than 0.1 seconds, and you wouldn't be able to see a noticeable improvement on that in using fread(), but with a distinct loss in flexibility.

    --
    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.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Note when using fgets() that a newline character (\n) will also be stored, so you may want to remove that

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    . . . but if you're using sscanf() or strtol(), you don't have to, because those functions (generally) ignore whitespace.

    Still, here's how you can remove the newline if you so choose:
    Code:
    #include <string.h>
    
    char buffer[BUFSIZ], *p;
    fgets(buffer, sizeof(buffer), stdin);
    if((p = strchr(buffer, '\n'))) *p = 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    alright cool, fgets does the trick. now, i used strtok to chop up the array into pieces, but how should i convert from char into float? thanks for your help.

  9. #9
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you really meant char * to float, then you could use strtod() to convert it to a double.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    8
    ok, thanks that seems to work well. one last problem: the file being read begins with a label of non-uniform length before proceeding to the numerical data.

    one string looks like this:
    200004_at 2501.67 2434.94 2424.03 2861.59 2537.48 2656.73

    is there some way i can skip over the label at the beginning so the string only consists of numbers? i'm having some trouble accounting for the labels not being all the same character length.

    thanks.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    If you can guarantee that there is no whitespace in the label, then that is easily done by reading the label string until you hit upon whitespace. Otherwise, this will be slightly more difficult.

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, 12: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, 08:32 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM