LOVE & HATE WITH C

Technical — acosta @ 10:25 am

There was a bug in a function I wrote yesterday to import large CSV (comma-delimited) files into a gsl_matrix structure. This annoyed the crap out of me because I knew the code was good but the control wouldn’t enter my while loops to import. And now, I know why.

My original code was …


 while ((s = fgets(buff, max_item_size * num_cols, data_file)) != NULL)
 {
   if (s[0] == '\\n' || s[0] == '#' || s[0] == ' ') continue;

   s = strtok(buff, ",");

   gsl_matrix_set(initial_data, num_rows_iterative,
     num_cols_iterative, atof(s));
   num_cols_iterative++;

   while (num_cols_iterative < num_cols)
   {
     s = strtok(NULL, ",");
     gsl_matrix_set(initial_data, num_rows_iterative,
       num_cols_iterative, atof(s));
     ++num_cols_iterative;
   }
   num_cols_iterative = 0;
   ++num_rows_iterative;
 }

I saw absolutely no problem with this at all, and was baffled as to why it wouldn't enter the loop. I had just looped over the same data with approximately the same function previously, and it had worked great. Of course, I forgot that I had already opened and read every line in 'data_file' in the same function previously.


 data_file = fopen(input_file, "r");

 while ((c = getc(data_file)) != '\\n')
 {
   if (c == ',') ++num_cols;
 }

 int max_item_size = 20;
 buff = malloc(max_item_size * num_cols);

 while ((r = fgets(buff, max_item_size * num_cols, data_file)) != NULL)
 {
   if (r[0] == '\\n' || r[0] == '#' || r[0] == ' ') continue;
   ++num_rows;
 }

A simple fclose and f(re)open between these calls fixed it up right nice. I don't know why I never thought of that as being a problem. I would imagine there's some nicer way to do this but honestly I don't much care at this point.

1 Comment »

  1. You know what really irks me? The shameless ways in which the Pixies (east coast brats) crib from The Replacements. Really annoying [is now a sentence].

    I like what you’ve done here Mr. Costa. You are an inscrutable artist. But I see through all that.

    Comment by Robert Peach — 11/1/2006 @ 3:33 pm

RSS feed for comments on this post.

Leave a comment

vdov.net is an anthony costa production. ownership of the content provided is retained by the author and by vdov.net.