Simple comma delimited text file

This is a discussion on Simple comma delimited text file within the C Programming forums, part of the General Programming Boards category; Hi, I have a simple comma delimited text file full of doubles such as 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 1,2,3,4,5 ... ... ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    16

    Simple comma delimited text file

    Hi,

    I have a simple comma delimited text file full of doubles such as

    1,2,3,4,5
    1,2,3,4,5
    1,2,3,4,5
    1,2,3,4,5
    ...
    ...
    ...etc

    How would I read each "column" into an array in C?

    I've tried something like this:
    Code:
    for (i = 0; i < num_rows; i++) 
    {
        fscanf(inf1, "%lf,%lf,%lf,%lf,%lf\n", &a[i], &b[i], &c[i], &d[i], &e[i]);
    }
    but the numbers are nonsense. I'm thinking that it's the newline that is messing it up. Any Suggestions?

    Thanks!

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    The best way would be to read each line with fgets and then convert each number in another loop with strtod(). That way you can actually verify that the numbers are within the correct range. Between each number, you can use a loop to consume whitespace and the commas (strtod will consume initial whitespace but not commas).

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    Code:
    for (i = 0; i < num_rows; i++) 
    {
        fscanf(inf1, "%lf%*c%lf%*c%lf%*c%lf%*c%lf%*c", &a[i], &b[i], &c[i], &d[i], &e[i]);
    }
    Try that.


    you shouldn't put the newline in when you are using fscanf and the like.
    Last edited by KBriggs; 06-22-2010 at 02:24 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    16
    hmmm the %*c didn't work.

    I'm not familiar with strtod()- looking it up now.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What does "didn't work" mean? You should check the return value of fscanf. * means to scan but not save, and c means to read a character. The *scanf functions take specifically formatted data. If your file is actually formatted in a predictable manner, then it should work fine, assuming you match said format.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Jun 2009
    Posts
    452
    In the code snippet I posted above there is 1 too many %*c. SOrry about that. Just delete the one off the end and it should work.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    I've never seen "%*c" before. '*' is used when an extra argument is passed for the number of chars expected. I don't see any extra args that are width related.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    No. I already said what * is for. It's to scan-but-not-store.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    16
    The %*c just produced more nonsense numbers. I tried it with and without the last %*c. I assumed the last one was for the newline. How does that handle the newline then?

    Thanks!

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    16
    I was able to get it to work using strtok() and this tutorial: C | /* StudentRec.c a simple stud - Sample demo to parse CSV text - JSENSYNR - Pastebin.com.

    Thanks for all your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 11:04 AM
  2. Replies: 8
    Last Post: 05-05-2010, 03:43 PM
  3. Replies: 19
    Last Post: 05-30-2007, 06:47 PM
  4. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

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