Reading a comma separated file

This is a discussion on Reading a comma separated file within the C Programming forums, part of the General Programming Boards category; I have a very simple question, but I am rather inexperienced in C so I can't figure it out. I ...

  1. #1
    nwr
    nwr is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    5

    Reading a comma separated file

    I have a very simple question, but I am rather inexperienced in C so I can't figure it out.

    I have a bunch of comma separated files, and each field is in quotation marks. Individual records are separated by carriage returns. I want to be able to to go a given record (a specific line in the file) and the read the data in a specific field in that record into an int or a char depending on whether the entry in that field is a number or text.

    So, how can I do this?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Unless all the lines are the same length, you'd have to record the position of each newline so you could seek to the right position in the file. Or you could read in the whole file into an array and address a specific line by the array element.
    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.

  3. #3
    nwr
    nwr is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    The lines all have the same number of fields.

    I am asking this question on a much more basic level. I don't even know how to succcessfully read a character from a file into memory. I know I have to use fopen(filename, r) but I don't know anything more than that.

    Thanks.

  4. #4
    nwr
    nwr is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Reading the whole file into an array might not be a bad idea, except the files are pretty large.

    The best way might be to go to a certain line and read the whole line into memory. If someone can help me out with the syntax of such an operation, I would be very grateful.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Well, if every line is, say, 10 characters long, then to access the fifth line you'd fseek to position 40, then read 10 characters in, probably with fgets():
    Code:
    void get_line(FILE *fp, int n, char *s) {
        fseek(fp, 10 * n, SEEK_SET);
        fgets(s, 9, fp);  /* not 10, to exclude the newline */
    }
    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.

  6. #6
    nwr
    nwr is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Unfortunately each line is not the same number of characters long. It's the same number of fields long, so I guess I could have it count commas, but I figured there would be a niceer way of doing it. Maybe I shouldn't be using C?

    Anyway, is it possible for me to read the entire file into an array that is still organized the way my file is set up?

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Maybe I shouldn't be using C?
    Any language would have the same problem, unless it read in the file and recorded the positions of the newline for you.

    Anyway, is it possible for me to read the entire file into an array that is still organized the way my file is set up?
    Yes, read in each line with fgets() and store it in an array:
    Code:
    char data[LINES][BUFSIZ];
    int line = 0;
    FILE *fp = fopen("file.txt", "rt");
    
    while(fgets(buffer[line++], BUFSIZ, fp));
    
    /* now data[] contains every line of the file; lines is how many lines were read in */
    (Note that I don't do any error checking in that code.)

    Once you have picked the line you want, you could use sscanf() to parse numbers or whatever from the string.

    [edit] Yes, I meant data[]. Oops. [/edit]
    Last edited by dwks; 08-05-2006 at 02:31 PM.
    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
    nwr
    nwr is offline
    Registered User
    Join Date
    Aug 2006
    Posts
    5
    Where you wrote buffer[line++], I think you meant data[line++], right?

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by nwr
    I have a bunch of comma separated files, and each field is in quotation marks. Individual records are separated by carriage returns. I want to be able to to go a given record (a specific line in the file) and the read the data in a specific field in that record into an int or a char depending on whether the entry in that field is a number or text.

    So, how can I do this?
    Declare a char array large enough for the longest line, and a counter initialized to zero. Read each line in a loop, incrementing the counter each time -- just discard each line of text you don't want and break the loop when you've gotten the the line you wanted. Then parse the line.

    But this is where it may get a little sticky. Since each field is in quotation marks, does this mean that commas may be embedded in a field?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Where you wrote buffer[line++], I think you meant data[line++], right?
    He put that there on purpose, to make sure you did your share of homework and not just lounge around waiting to be spoonfed.

    Adding in code to handle quotes shouldn't be an issue. It's even feasible to check for escaped quotes (eg. {\"} as a literal quote) without hurting performance. You're going to iterate over those characters anyway in a naive comma search.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  11. #11
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    498
    Look, if you don't even know how to use fgets(), then why are you trying to figure out how to parse? First learn how to read a simple text file, and THEN start learning how to parse strings/get newlines, etc, etc..

  12. #12
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Do you absolutely have to use C? Problems like these just scream "Perl". There are tons of CSV- and DSV-parsing modules out there.

    In fact I managed to throw something together in 8 minutes, most of which spend find(1)'ing the CPAN module, and I haven't even read chapter 5 of the Llama Book yet.
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 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. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

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