Need help reading and writing from binary file

AI Thread Summary
Writing a large amount of data to a binary file requires knowing the total number of records to be recorded, which is challenging in stochastic simulations like chemical reactions. The suggested approach is to reserve the first four bytes of the file for the record count, writing placeholder data initially and updating it after the simulation completes. Using functions like fseek and fwrite can help manage file positioning and data writing, but it’s crucial to keep track of the total bytes written during the process. Reading the file can be done using standard methods, as EOF indicates the end of the file. Ultimately, managing a buffer or an internal counter can simplify the writing process and ensure accurate data handling.
ptabor
Messages
14
Reaction score
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
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.
 
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.
 
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top