Thread: CSV read error

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

    CSV read error

    while reading a csv file using following code, abnormal operation error comes:
    Code:
    int main(void)
    {
       const char filename[] = "d:\\tc\\csv1.csv";
       FILE *file = fopen(filename, "r");
           char scname[11];
         char sctype[3];
         double oval;
         double hval;
         double lval;
         double lclos;
         double qty;
         double idno;
         char tdate[11];
         int j;
    //csv1.csv file has 9 fields as above
     clrscr();
     while(fscanf(file,"%10s %2s %f %f %f %f %f %f %10s",scname,sctype,&oval,&hval,&lval,&lclos,&qty,&idno,tdate) != EOF)
      {
        printf("%10s %2s %f %f %f %f %f %f %10s\n",scname,sctype,oval,hval,lval,lclos,qty,idno,tdate);
      }
    fclose(file);
    return 0;
    }
    i have tried %s, %e specifiers in fscanf;
    if i use %s then whole line is stored in first field viz. scname;

    is this the correct way to read csv file?
    i want to associate each field of each line in csv with the variables used above.
    thanks for your valuable replies in advance

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     while(fscanf(file,"%10s,%2s,%f,%f,%f,%f,%f,%f,%10s",scname,sctype,&oval,&hval,&lval,&lclos,&qty,&idno,tdate) > 8)
    It's pattern matching... in this case the pattern is values separated by comas...

    Look up fscanf() in your C library documentation for why >8 instead of EOF.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    control does not enter inside the loop with fscanf > 8 condition.

    with != EOF, the error that comes is "floating point error:domain. abnormal program termination"

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Well, you should check what it is if it is <8, because there are 9 specifiers there.

    The reason you get the error with != EOF is probably because if the return value is >=0 you do enter the loop.

    I notice there are no commas in your template, but you refer to csv data.

    if i use %s then whole line is stored in first field viz. scname;
    That would happen if there is no whitespace, because it is csv data ("bob,32,alpha,32.4,11" is one %s). You could use:

    Code:
    fscanf("%[^,],%[^,],%f" etc
    [] is a set stored in a char*. ^ means "not", so %[^,] will grab anything upto a comma.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by pcrana View Post
    control does not enter inside the loop with fscanf > 8 condition.

    with != EOF, the error that comes is "floating point error:domain. abnormal program termination"
    If your variables are doubles, you need to use %lf not %f ... That's most likely the biggest problem right now.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Does Turbo C support %lf? Because judging by this line

    Code:
    const char filename[] = "d:\\tc\\csv1.csv";
    I'm betting the "tc" part of that is Turbo C.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rags_to_riches View Post
    Does Turbo C support %lf? Because judging by this line

    Code:
    const char filename[] = "d:\\tc\\csv1.csv";
    I'm betting the "tc" part of that is Turbo C.
    Good question... the 16 bit versions did not... don't know about the TC++ versions.

    In any event, our OP friend might just want to consider an upgrade to something from this millenium.

  8. #8
    Registered User
    Join Date
    Jan 2012
    Posts
    3
    thanks to all for replies.
    got the solution using [^,]
    and also changing double to float and %f in conversion specifier solved the problem
    thanks once again

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    None of this is the right way to parse a CSV file. A csv file is allowed to have double-quotes around the data in a column, and that string data is allowed to contain commas. There is no valid way to parse in a generic CSV file by matching it against a predefined pattern.
    You need to process the incomming data character by character within a state machine that takes care of commas embedded within a column etc.
    Have a good read of Comma-separated values - Wikipedia, the free encyclopedia
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. read from file error
    By xniinja in forum C Programming
    Replies: 5
    Last Post: 06-17-2010, 03:10 PM
  2. sscanf read error
    By me_ansh in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 03:40 AM
  3. SDL read error
    By MadCow257 in forum C++ Programming
    Replies: 3
    Last Post: 12-27-2005, 04:10 PM
  4. Error!- Cannot open .rc! READ!!
    By oobootsy1 in forum C++ Programming
    Replies: 3
    Last Post: 07-30-2003, 05:06 AM
  5. Memory cannot be read error?
    By adam78 in forum C Programming
    Replies: 3
    Last Post: 12-03-2001, 03:25 PM