The following function is not working i.e. it exits:
fread(buf, 1, 4, stdin);
buf[4] = '\0';
if (strcmp((char*)buf, "data")) exit(EXIT_FAILURE);
I think if I can manually push fread farther down the stream it will eventually hit “data”.
In other words how do I increment fread so that it skips bytes.
Examples of code always appreciated.
Thanks!
EDIT 1
Basically I’m parsing the header of a wav file on the iPhone. It is giving me some trouble and I believe it has to do with the way apple formats its audio files. Someone recommended to me running through the stream until I get “data” and then moving forward from there.
I hope this clarify things.
EDIT 2
Here is documentation as to how the wav file header should look like, but I’m wondering if the way apple formats theirs makes this inaccurate.
You will notice that ‘data’ is offset by 36 which is a multiple of four.
This works in a stream like fashion and does what I think you want:
This works by using a buffer I’ve called
strand rotating the positions around in it. It will work untildataappears.Be aware it reads binary data, not text. So anything on stdin gets read, newlines included. However, if you adapt that to a file handle that shouldn’t be a problem.
You can probably include this. The problem with using
freadis that by design:Therefore if you advance by 4 bytes at a time, unless your data is exactly a multiple of 4 from the start of the data, you’re going to miss it. For example:
Fails if you read 4 bytes at a time.
Now, given this is a documented file format, are there not some header specs somewhere that tell you exactly how wide the fields are on the header? Or at least where they vary, such that you can read them off appropriately? Reading until
dataworks, but isn’t elegant, really.Or, better still, I’m sure there must be a library for doing this somewhere.
Edit In response to the header of the wave file, since it is fixed and not that large, read the whole thing into a buffer.
Don’t forget to free. At this point, you have the entire header extracted. I’ve used
uint8_tto definitely be8bits. At this stage you can pull some interesting tricks, like casting that data to a struct. Just be aware of the endianness of the fields.From then on, the stream is available to you in chunks, I believe. The first thing you need to do is this:
That’ll grab you the data of that particular chunk. Assuming you’re using a little endian system, You ought to be able to use
chkszdirectly as an integer at this point, so now you can do:Into which you can read the data:
This is of course assuming that the Apple wave format is the one described. Now, from that page:
I’ve given you instructions that if used in a continual loop until there’s nothing more on the stream, will allow you to read any number of data chunks a-la RIFF. You then need to process the data you get appropriately to break it down; i.e. split your data chunk that you read in up appropriately. If this is the only format you expect to read, you could just ignore additional chunks.
Now, the problem remains, what is the apple format and to be honest I’ve no idea!