By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,688 Members | 1,107 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,688 IT Pros & Developers. It's quick & easy.

dynamic content of a binary file

P: n/a
I have to output data into a binary file, that will contain data
coming from a four channel measurement instrument.
Since those data have to be read from another C program somewhere
else, the reading program must know how many channels have been
acquired, date, time, and so on. I mean that the position of each
datum is not fixed in the file but depends on the conditions when
acquired.
That is, I need something like a header in the file to interpret it. I
thought to use ascii header, but I do not want to mix ascii and bin in
the file.
What could be a simple solution?
Any suggestion appreciated.

Apr 2 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
bwv539 wrote:
I have to output data into a binary file, that will contain data
coming from a four channel measurement instrument.
Since those data have to be read from another C program somewhere
else, the reading program must know how many channels have been
acquired, date, time, and so on. I mean that the position of each
datum is not fixed in the file but depends on the conditions when
acquired.
That is, I need something like a header in the file to interpret it. I
thought to use ascii header, but I do not want to mix ascii and bin in
the file.
What could be a simple solution?
Any suggestion appreciated.
IOW, you need to impose some structure to the file. Well, one option
is to divide, (or group), your incoming data into an array of
structures and then write them out to the file. Then reading back the
structures could be straightforward. But a binary file is not
necessarily portable between implementations and systems. For maximum
potability a text file is the better option.

You could have a seperate structure member that just notes the members
that have valid values. A possible struct might be:

struct data_set {
data_type channel1;
/* ... */
time_type date_and_time;
/* other members */
};

You could even make the structure an "opaque" one and provide
functions for creating, reading and writing them.

Apr 2 '07 #2

P: n/a
In article <11**********************@b75g2000hsg.googlegroups .com>,
bwv539 <bw****@yahoo.comwrote:
>That is, I need something like a header in the file to interpret it. I
thought to use ascii header, but I do not want to mix ascii and bin in
the file.
What could be a simple solution?
ASCII is just a particular interpretation of 8 bit binary characters.
As far as binary file theory is concerned, there is no difference
(other than efficiency) between having a binary string
\x54\x59\x50\x45\x33\x42 or a binary string \x3B .
The first of the two happens to be interpretable in ASCII as
'TYPE3B', but really it's just a binary string of no more
(and no less) meaning to the file than the binary string \x3B.

The time to be concerned about ASCII vs binary (more correctly, text
streams vs binary streams) is in the treatment of line terminators
(e.g., \n ) and the treatment of binary 0's. If you were trying to mix
binary and text on the same logical line and read it back in a text
stream, you would need to be concerned in case the binary happened to
come out as binary 0 or happened to come out as one of the
implementation-dependant line terminators. But if you are consistant
in using binary streams, and wish to write out something that
is readable to humans when viewed as ASCII, you just have to worry
about whether the execution character set is ASCII
(because if it isn't, and you fputs("TYPE3B",stdout) then
it might not be ASCII's "T" and "Y" and so on that were written out.)
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Apr 2 '07 #3

P: n/a
bwv539 wrote:
I have to output data into a binary file, that will contain data
coming from a four channel measurement instrument.
Since those data have to be read from another C program somewhere
else, the reading program must know how many channels have been
acquired, date, time, and so on. I mean that the position of each
datum is not fixed in the file but depends on the conditions when
acquired.
That is, I need something like a header in the file to interpret it. I
thought to use ascii header, but I do not want to mix ascii and bin in
the file.
What could be a simple solution?
Any suggestion appreciated.
My opinion? Use a small database like DBM or SQLite to store your data.
This is a perfect companion to a datalogger. As suggested
else-thread, you can also encode and store the data as text, which will
force you to come up with some sort of schema or format.

You can do this by coming up with your own binary format and writing the
routines to provide a normalized interface to that file, but by the time
you do that you've pretty much built a special-purpose database system
anyway.

Just my opinion, but I'm biased against reinventing wheels and toasters.
Apr 2 '07 #4

P: n/a
Thanks to all of you for the suggestions.
But, I have to confess that I made a mistake in the original post: the
data file generated by my C program, has to be read from a matlab
routine, not from a C program. I am not very familiar with Matlab,
someone else will write this routine, but I suppose there are not C
structures.
So, I need a header containing for example the name of the instrument
under test, date, time, environment informations, serial number, and
so on, and then a description of what follows: number of channels,
data types, etc.
I think I will end up writing a binary header with static size.
However, if you have more suggestions, please tell me.

Apr 3 '07 #5

P: n/a

bwv539 wrote:
Thanks to all of you for the suggestions.
But, I have to confess that I made a mistake in the original post: the
data file generated by my C program, has to be read from a matlab
routine, not from a C program. I am not very familiar with Matlab,
someone else will write this routine, but I suppose there are not C
structures.
Though it's possible to correctly interpret binary files generated by
one program with another program, you might find it more
straightforward to use plain text files. The small reductions in space
and time made by using binary files is usually not very important.

It's also quite probable that Matlab has the ability to read files in
a variety of formats. You might consider generating your data file in
one of these formats.

<snip>

Apr 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.