Binary files 
Author Message
 Binary files

What is the most efficient way of writing binary data to disk ?

I want to write a set of structures but even after compressing the resultant
file it takes up too much space.

The structure to write to file is as follows

struct
{
    long
    char[256]
    char[256]
    char[256]
    char[256]
    char[256]
    char[256]
    size_t

Quote:
}

I am writing the file using

    fopen(filename,"wb");

    fwrite(struct, sizeof(struct), 1, stream);

I have thought of using pointers instead of fixed arrays but there would be
the problem of reading the file without knowing the size of each array.  The
size could be written before each array but this seems a bit messy.

Any ideas?

Thanks in advance

--
----------------------------------------------
Eliott R D Mitchelmore



Mon, 13 Nov 2000 03:00:00 GMT  
 Binary files

   I have thought of using pointers instead of fixed arrays but there would be
   the problem of reading the file without knowing the size of each array.  The
   size could be written before each array but this seems a bit messy.

That's the typical way it's done, though.  There are other ways, of
course, but most of them are much more complicated.  One exception: if
you know that each char[] element is null-terminated (or terminated
with some other constant value), you can just write them out like
that.

Text files are often a better choice than binary files, BTW, just
because they're easier to read and modify by hand.



Mon, 13 Nov 2000 03:00:00 GMT  
 Binary files

Quote:

> What is the most efficient way of writing binary data to disk ?

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
How are you defining "efficient"?  By speed or space?

Quote:
> I want to write a set of structures but even after compressing the resultant
> file it takes up too much space.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The fastest method to write lots of structures is to write as large
of a block as possible.  Say you had 10 structures to write, then
use:
    fwrite(array, sizeof(struct), 10, stream);

Another method is to use Direct Memory Access (DMA) if your platform
has this feature.

Quote:
> The structure to write to file is as follows

> struct
> {
>     long
>     char[256]
>     char[256]
>     char[256]
>     char[256]
>     char[256]
>     char[256]
>     size_t
> }

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If your looking for compressing the file length, try using variable
length records.  This will slow down your program, but you do save
space in the file.

struct var_str_t
{
   unsigned int length;
   char *       string;

Quote:
};

Using this method, write the length of data, followed by the data.

Here are some questions to ask yourself:
1. Is all this data REALLY necessary?

2. Redundancy:
2.1. Is there any redundant data?
2.2. Can the redundant data not be generated?
2.3. Can the redundant data be compressed (stored once)?

3. Can the data be queued, then written as a big chunk 'o data?

--
Thomas Matthews



Mon, 13 Nov 2000 03:00:00 GMT  
 Binary files

On Thu, 28 May 1998 13:09:09 +0100, "Eliott Mitchelmore"

Quote:

>What is the most efficient way of writing binary data to disk ?

>I want to write a set of structures but even after compressing the resultant
>file it takes up too much space.

>The structure to write to file is as follows

>struct
>{
>    long
>    char[256]
>    char[256]
>    char[256]
>    char[256]
>    char[256]
>    char[256]
>    size_t
>}

>I am writing the file using

>    fopen(filename,"wb");

>    fwrite(struct, sizeof(struct), 1, stream);

>I have thought of using pointers instead of fixed arrays but there would be
>the problem of reading the file without knowing the size of each array.  The
>size could be written before each array but this seems a bit messy.

Let's see: you want to compress the data, and yet you want to be able
to read it with a simple fread()?

I hope you realize that your requirement is impossible to meet. Being
able to fread() the structure depends on the fact that the exact
memory representation of the structure has been transparently recorded
in the file.

If you want to achieve any compression, however simple, you will have
to write routines to perform encoding and decoding of the data
between the memory representation and the condensed representation.



Mon, 13 Nov 2000 03:00:00 GMT  
 Binary files

Quote:

>Let's see: you want to compress the data, and yet you want to be able
>to read it with a simple fread()?

>I hope you realize that your requirement is impossible to meet.

Not impossible at all.  The file is simply packed and unpacked using the
UNIX system call.

--
----------------------------------------------
Eliott R D Mitchelmore



Tue, 14 Nov 2000 03:00:00 GMT  
 Binary files



Quote:

>>Let's see: you want to compress the data, and yet you want to be able
>>to read it with a simple fread()?

>>I hope you realize that your requirement is impossible to meet.

>Not impossible at all.  The file is simply packed and unpacked using the
>UNIX system call.

What Unix system call? Also consider that the C language doesn't support
any Unix system calls.

--
-----------------------------------------


-----------------------------------------



Tue, 14 Nov 2000 03:00:00 GMT  
 Binary files



#>>
#>>Let's see: you want to compress the data, and yet you want to be able
#>>to read it with a simple fread()?
#>>
#>>I hope you realize that your requirement is impossible to meet.
#>
#>Not impossible at all.  The file is simply packed and unpacked using the
#>UNIX system call.

# What Unix system call? Also consider that the C language doesn't support
# any Unix system calls.

signal?

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh
SIGSIG -- signature too long (core dumped)



Tue, 14 Nov 2000 03:00:00 GMT  
 Binary files

On Fri, 29 May 1998 09:40:51 +0100, "Eliott Mitchelmore"

Quote:

>>Let's see: you want to compress the data, and yet you want to be able
>>to read it with a simple fread()?

>>I hope you realize that your requirement is impossible to meet.

>Not impossible at all.  The file is simply packed and unpacked using the
>UNIX system call.

I'm afraid I don't follow. Are you referring to using the C system()
function to run a compression utility? The term ``system call''
usually refers to one of the many entry points into an operating
system which provide useful services to applications.

In any case, you seem to be hinting at a platform-dependent solution
rather than one which uses standard C.



Tue, 14 Nov 2000 03:00:00 GMT  
 Binary files



...

Quote:
># What Unix system call? Also consider that the C language doesn't support
># any Unix system calls.

>signal?

That is of course a standard C library function that Unix supports. :-)

Seriously though, C's support for signal() is much more restrictive than
Unix's (for example in the range of signals supported). These days it is
up to Unix implementations to be compatible with C's definition rather
than the other way around.

--
-----------------------------------------


-----------------------------------------



Wed, 15 Nov 2000 03:00:00 GMT  
 Binary files



#># What Unix system call? Also consider that the C language doesn't support
#># any Unix system calls.
#>
#>signal?

# That is of course a standard C library function that Unix supports. :-)
# Seriously though, C's support for signal() is much more restrictive than
# Unix's (for example in the range of signals supported). These days it is
# up to Unix implementations to be compatible with C's definition rather
# than the other way around.

Indeed. I'd *love* to see sigaction in C9X... But signal stuff is a mess
to standardize, I reckon.

Regards,

        Jens
--
Jens Schweikhardt  http://www.shuttle.de/schweikh
SIGSIG -- signature too long (core dumped)



Wed, 15 Nov 2000 03:00:00 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. reading a binary file back to binary

2. how can search binary code in binary file?

3. HELP: Binary Large Objects (need to store binary file w/in database)

4. HELP: Binary Large Objects (need to store binary file w/in database)

5. putting end of file character in binary file (cutting the file short)

6. Copying files with getc to putc doesn seem to like binary files:

7. Binary file read to New text file save grief

8. how to copy file(binary file) ?

9. HELP : Deleting bytes from binary file, using FILE *

10. Binary file or TEXT file?

11. .cpp file corrupted - recovered and appears as binary file - HELP

12. Convert binary file to text file format

 

 
Powered by phpBB® Forum Software