SDFat: Writing and reading binary data file using streams

I’m working on a project where at setup time I need to read a .CSV file from a card, parse each line into a series of long ints, and display it on the serial. So far so good, with the aid of the SDFat → readCSV example.

As I read each line, I want then to write it back to a different file on the card, as binary data rather than ascii text.
So for example;
01, 2, 3, 004
02, 03, 04, 5[/table]

would get written to the new file as bytes as follows;
00,00,00,01,00,00,00,02,00,00,00,03,00,00,00,04,00,00,00,02,00,00,00,03,00,00,00,04,00,00,00,05
(byte ordering might be a bit wrong here, but I hope you understand)

if I just use the stream to output the longs i’ll get an acsii representation of the number again I think;

long l1,l2,l3,l4;
     
  ifstream sdin(CSVfileName);
  ofstream sdout(BINfileName);

  // read a line from the .CSV file & parse it
  while (sdin >> l1 >> c1 >> l2 >> c2 >> l3 >> c3 >> l4 )
 {
     // this writes an ascii representation of the numbers to the serial port
     cout  << l1 << l2  << l3 << l4 << endl;  
     // this writes an ascii representation of the to a file I think.
     sdout  << l1 << l2  << l3 << l4;    
 }

etc...

I could of course convert each long into something that when written as ascii, writes the correct bytes, but i’m sure there is an easier way, but can’t seem to figure it out - can anyone help me?

The reason I want the binary file from the CSV, is that later, at run time, I want fast access directly to the numbers as a lookup, and hope to be able to seek directly to the long value I want rather than opening and parsing the file each time. (Binary file size will be > arduino memory size)

I hope that makes some sense…
Mark B

[Arduino mega2560 with extra ram (64K+), IDE 1.5.6 c couple of nightly’s ago, SD_FAT_VERSION 20120719, 2GB generic SD card in LCStudio board)

I didn't implement read() and write() in file streams. The default for streams is text mode which inserts a CR before LF for write() and removes the CR before a LF on read(). This would be really slow and not very useful on Arduino.

In a full implementation of streams you can open a file in binary mode and use read() and write().

The SdFile and SdBaseFile classes do binary reads() and writes(). Read the CSV file with a ifstream and write the binary file with a SdBaseFile using write();

Read the binary file with SdBaseFile using read().

Yes, that makes sense. Thanks for you help, and the excellent library.

MB.