Need help reading and writing from binary file

In summary, the programmer is trying to write data to a binary file, but doesn't know how much data they wrote until the program finishes executing. They've tried using fseek() and fwrite(), but they're not working. They want to know how much data was written to the file, so they can create an array in another program and read in the data.
  • #1
ptabor
15
0
I'm writing a large amount of data to a binary file. Trouble is, I don't know how much until the program finishes executing.

Given this, I'd like to rewind back to the beginning of the binary file and write an element indicating how many records I wrote to the binary file - this way I can create arrays in another program and read in the data to do some fun stuff with it.

I've tried:
fseek(filename, sizeof(int), SEEK_SET);
fwrite(&data, sizeof(int), 1, filename);

to no avail. Can someone please assist?
 
Technology news on Phys.org
  • #2
When reading any binary file (or even ASCII), the end of the file will be indicated with an EOF (end of file). There's no need to append the file size. For example.

Code:
char buffer[100];
while( !feof( stream ) )
   {
      /* Attempt to read in 10 bytes: */
      count = fread( buffer, sizeof( char ), 100, stream );
   }
I'm also curious about this application. Why is it that you do not know the amount of bytes you are going to write. The write operations should be from a buffer, some sort of array whose size ( sizeof(arr) ) can be found.
 
  • #3
It's a program to stochastically simulate chemical reactions.

Since it's stochastic, it is a probabilistic problem and there is no way to know beforehand how many reactions you will have in any given interval.

Basically, each time a reaction occurs I'm writing the time at which it occurs, and the reaction number to a binary file. At the end of the simulation I'd like to output the total number of reactions to the beginning of the file so I can write another section of code to parse this data - ie so I can know the state of the system at any point in time.
 
  • #4
Unless the file you are writing is very very large, you can write all of this information into a buffer. Or of course, you can keep track of how many characters you have written internally in a variable. fwrite() returns the number of bytes it has written.

As far as I know however, it is not possible to shift the bytes in the file. If after all my suggestions you still for some reason want to write the written number of bytes into the file "header", then reserve the first four bytes for this purpose. I'm assuming that your file would not be larger than (2^32) bytes (~4200 Gigabytes). Write junk data to it first and then append it.
 

1. What is a binary file?

A binary file is a computer file that contains data in a binary format, meaning it contains only 0s and 1s. It is different from a text file, which contains human-readable characters.

2. Why do I need help reading and writing from a binary file?

Reading and writing from a binary file can be more complex than working with a text file because the data is stored in a different format. In order to properly manipulate the data in a binary file, you may need help understanding the structure and methods for reading and writing it.

3. How do I read from a binary file?

To read from a binary file, you need to open the file in binary mode and use functions such as fread() or fscanf() to read the data. It is important to understand the structure of the data in the file in order to read it correctly.

4. Can I edit a binary file?

Yes, you can edit a binary file by opening it in binary mode and using functions such as fwrite() or fprintf() to write data to the file. However, it is important to be careful when editing a binary file as any changes made can affect the entire structure of the file.

5. Are there any tools or libraries that can help with reading and writing from a binary file?

Yes, there are various tools and libraries available that can help with reading and writing from a binary file, such as the standard C library functions for file input/output, or more advanced libraries like libbinary or libbfio.

Similar threads

Replies
10
Views
958
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
5K
  • Programming and Computer Science
Replies
22
Views
919
  • Programming and Computer Science
Replies
4
Views
741
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
3
Replies
75
Views
4K
Back
Top