How to read/write binary data from a file on an SD card

I have not been able to successfully figure out how to read binary data from a file on an SD card. I've tried the SDFat library and the arduino 'built in' SD functions. I imagine that both are capable of this simple task, but I haven't been able to find how. There are plenty of examples for how to read and write ascii characters, which is easy enough from the examples available, but I haven't found one for how to read (for example) two byte signed integer data from a file on an SD card. I did find some posts of other people also having trouble with this, so I hope that this will be useful to the community at large. Once I figure out how to read a single integer, I'm going to read both raw data from a headerless binary file, and the data section of a .wav file to create audio output directly on the arduino, without the wave shield. Any help on the latter also gratefully appreciated.

Thanks for your help

I've tried the SDFat library and the arduino 'built in' SD functions.

Where's the code?

There are plenty of examples for how to read and write ascii characters, which is easy enough from the examples available, but I haven't found one for how to read (for example) two byte signed integer data from a file on an SD card.

The process is exactly the same. Read and store the most significant byte. Read and store the least significant byte. Shift the MSB 8 places left and add the LSB.

I did find some posts of other people also having trouble with this

I can't imagine why. It's trivial.

Once I figure out how to read a single integer, I'm going to read both raw data from a headerless binary file, and the data section of a .wav file to create audio output directly on the arduino, without the wave shield.

Yeah? Good luck with that.

One of the speed tests with SDlib writes a binary file and checks it back. You should find what you need in there.

PaulS: Can you provide 'trivial' code that does the "Read and store the most significant byte. Read and store the least significant byte. Shift the MSB 8 places left and add the LSB."

As to 'triviality', you may or may not be aware that on different platforms there are enough different ways of data being stored (eg which byte is LSB) that it can be pretty time consuming to try to work out, especially if you have several different data types to deal with. I guess I was presuming that the libraries had a function to handle this, and it wouldn't be necessary to write one.

Thanks

ardnut: can you let me know the name of the library and function for the speed test you're referring to? I've already tried to go through all of the SD-related functions that I could find to find a binary read example, and I didn't find it.

Thanks

PaulS: Can you provide 'trivial' code that does the "Read and store the most significant byte. Read and store the least significant byte. Shift the MSB 8 places left and add the LSB."

byte msb = File.read();
byte lsb = File.read();

int val = (msb << 8) + lsb;

As to 'triviality', you may or may not be aware that on different platforms there are enough different ways of data being stored (eg which byte is LSB) that it can be pretty time consuming to try to work out, especially if you have several different data types to deal with.

Ah, yes, the joys of trying to share binary data across different platforms.

install SDlib then look in examples for "bench".

/*

  • This sketch is a simple binary write/read benchmark.
    */

Thanks very much to the commenters for your help.
I've gotten the binary reading to work.
As it turns out -- and often does -- this trivial little detail was one of the most time consuming parts of the whole project. Considerably more so than writing the code.

Thanks again.