Creating .CSV file in C

Discussion in 'Mac Programming' started by LtRammstein, May 6, 2009.

  1. LtRammstein macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #1
    Hey all!

    I am wanting to create a .CSV file in C via a microcontroller (S08JM60 to be exact).

    I was wondering if there's anything special that I have to do to make it work, or if there's a UML Diagram of what to do for it. Because I have a feeling that it's a standardized file type.

    Any help is greatly appreciated!

    EDIT:

    If you want to know the specifications, I am grabbing sensor data every 30 seconds, so I want to be able to store the file once there is no power running to it. I plan on having a small battery backup to it to keep it powered to store the data when the device is unplugged.
     
  2. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #2
    CSV - Comma Separated Values

    So there's really only formatting involved

    <value1>,<value2>, ... <valueN>

    No special sauce needed...
     
  3. LtRammstein thread starter macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #3
    I understand that, but I'm wondering if there's a special algorithm to use.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    Either a single or perhaps nested loop depending on your data. Each line will be a record, so I assume you'll have a number of these to loop over. As for the fields, this depends on how you have the data stored in memory. If it's just an array, an inner loop may be appropriate, otherwise you may be able to achieve this with one print statement.

    -Lee
     
  5. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #5
    Hi

    Reading CSV files that other people have created can get a bit tricky at times, but if you are only dealing with your own data then it should be straight forward. One thing that may be of use is that you don't have to use a comma as the separator character. So if your data contains ',' characters in it, you could change to using '@' or '^' as the separtor for example. This is far easier than quoting your data and escaping any embedded quotes in the data itself.

    ß e n
     
  6. macsmurf macrumors 65816

    macsmurf

    Joined:
    Aug 3, 2007
    #6
    CSV isn't really standardized AFAIK. On the surface it is a simple format but depending on the data one might have to handle escaping of the delimiter, escaping of the escape character (windows uses " in a fairly esoteric way) and multiline cell data. Also bear in mind that different OSes use different ways to specify end of line (Windows: \r\n, Unix: \n and OS X: \r just to make it interesting). In addition, the character set isn't specified. I believe windows uses windows 1252 which is almost the same as latin-1 (ISO-8859-1).

    That's not to say you shouldn't use it :) It all depends on what you want to use the data for.
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    In a recent project we ended up going with character 1 for the field separator and 2 for escaping line breaks in field values. These are very difficult to type, and for our purposes if they showed up in the data they could safely be stripped.

    -Lee
     
  8. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #8
    But then it wouldn't be a CSV file.

    CSV files by nature are portable. If you swap between systems, typically the transmission (FTP or whatever) usually manages the EOL issue.

    The <values> are formatted any way you want. Typically though, they are meant to be loaded into databases or spreadsheets.

    I did forget the EOL in the initial post though.

    <value1>,<value2>, ... <valueN>\n

    Usually when reading from memory it's just a loop

    do{

    getNextValueSet(&floatVal, &stringVal, &intVal);

    fprintf(myfile, "%5.2f, %s, %d\n", floatVal, stringVal, intVal);

    if (no more rows)
    done = 1;

    }while (!done);


    Using any other field separator characters doesn't make it a CSV file. The only thing you have to watch for is string values. Typically special characters in strings screw up databases (quotes ", AT signs @, commas, etc. ), so make sure to take into consideration where this data is going.

    In the past, I've typically used CSV and Pipe delimited "|" files as they seem to be the most common.
     
  9. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    Don't forget you can have an eol within the the variables too.

    ß e n
     
  10. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Yes, of course. But the OP didn't say how this file is going to be consumed. If there is some existing code, etc. that this file will be parsed by, they need to write the file to the specifications of that code. I just wanted to point out another option that doesn't send one into escape-sequence madness.

    -Lee
     
  11. LtRammstein thread starter macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #11
    Thanks for the replies.

    I do want to clarify something, and the .CSV file will be read by a spreadsheet program, most likely Excel. I know Excel can import .CSV files easily, that's why I chose this file type.

    As far as I can tell from everyone's reponse, there is no standardized algorithm to use, so I just have to adapt it to my microcontroller OS.

    All I need to do now is get a NAND Flash module to be formatted for FAT32...
     

Share This Page