Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

LtRammstein

macrumors 6502a
Original poster
Jun 20, 2006
570
0
Denver, CO
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.
 
CSV - Comma Separated Values

So there's really only formatting involved

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

No special sauce needed...
 
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
 
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
 
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.
 
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
 
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

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.
 
But then it wouldn't be a CSV file.

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
 
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...
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.