dynamic content of a binary file

B

bwv539

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.
 
S

santosh

bwv539 said:
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.
 
W

Walter Roberson

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.)
 
C

Clever Monkey

bwv539 said:
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.
 
B

bwv539

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.
 
S

santosh

bwv539 said:
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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top