Using *.csv files with C code
By DPontes11 at Wed, Jan 7 2009 3:49pm |
Hi everyone
I'm new to the forum... :) and I was wondering if someone coud help me out. I've been trying to find in the Internet some help with this, but my efforts haven't payed off.
I need to know how to get the data in a csv file using C code. I know how to do this with R language, but I have no idea how it's done in C.
The idea is to translate the programs I've created in R language into C, so that the data processing might be faster (as I suspect that C will be faster than R).
Can anyone help with this? what type of libraries must I get to use csv, and what commands are used to work with the data within the files...?
Thanks in advance to anyone who might help me...
Cheers...



Re: Parsing CSV
If you are using Objective-C, you could use the code from my tutorial on CSV parsing. Even if you don't have Obj-C, you could probably adapt the code, simply replacing NSScanner with C methods like scanf.
Drew
---------------------------
Drew McCormack
http://www.maccoremac.com
http://www.macanics.net
http://www.macresearch.org
Speed of Processing vs. Speed of Coding
One thing you should weigh is the speed gains you might get from processing data in C, against the time it would take you to write the code. If you're not that familiar with C, the latter might take time.
So if you're likely to save several days or weeks processing data in C, then it sounds like a good tradeoff to me. Otherwise R (or Perl, or Python or...) is a good language.
As Drew mentioned, you probably want to use scanf in C. Most C tutorials should cover reading data with scanf.
http://www.cppreference.com/wiki/c/io/scanf
how about tcl/tk?
have you look for scripting languages as tcl ??
tcl is pretty simple to code and yet powerful. just check tcl.tk for the documentation and sample code....
or learn objective-C, it's easy, fun and pretty cool!!
How about calling C code from within R?
The alternative is to compile C code using 'R CMD SHLIB' and then call C functions from within R using .C or .Call. I do this regularly for statistical analyses with unavoidable nested loops - data preparation and summarising results is done in R, but most of the actual work is done in C (which is much faster than R for iterating). You then don't have to worry about reading the .csv file in C - you can just pass it an array with all the data as it expects to find it. See:
http://cran.r-project.org/doc/manuals/R-exts.html#Interface-functions-_002eC-and-_002eFortran
...for more info.
Hope this helps!
Matt
C and CSV
I use fgets() to read a file line by line. After that what you need to do depends on what you are trying to do.
You will most likely need to process each line character by character to find the delimiters and hence the 'field ' values. You will likely need functions such as atoi() and atof() to convert sub-strings into ints and floats. I use sprintf() or snprintf() to reconvert the ints etc back to a final output string (in my program I have to read a data file for forex prices in CSV format, manipulate the data including doing some calculations and then writing it back to another file in the same format - so I convert from string to floats/ints etc and then string it altogether and write it out).
You may be able to unpack the lines more simply if the format is fixed, then you can possibly sscanf() or just use pointer arithmetic to reach the appropriate parts of it.
I believe c++ has some more built in functionality than pure 'C', but I am not very familiar with C++.
I believe there is a good 3rd party library for Java if you can write that, and Python and Ruby may also work better. I plan to try rexx and / or rebol because data conversion is much less of an issue.