Changing .bin file's data in C -
As a sequence of strings I have a lot of data stored in the bin format. I randomly read any strokes And I want to be able to modify it in C I am trying with the following code but it does not work. Can anyone fix this for me?
Also, would it be possible to delete an intermediate structure from the file in the middle?
The code is below:
#include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Struct rec {int x, y, z; }; Zero f_rite () {int i; File * ptr_myfile; Structure RE my_record; Ptr_myfile = fopen ("test.bin", "w"); For (i = 0; i <5; i ++) {my_record.x = i; FILIT (& amp; my_record, sizeof (structure REC), 1, ptr_myfile); } Fclose (ptr_myfile); Return; } Zero f_read () {int i; File * ptr_myfile; Structure RE my_record; Ptr_myfile = fopen ("test.bin", "r"); For (i = 1; i & lt; = 5; i ++) {Fried (& my_record, sizeof (structure REC), 1, ptr_myfile); Printf ("% d \ n", my_record.x); } Printf ("\ n"); Fclose (ptr_myfile); Return; } Zero f_rerite () {int i; File * ptr_myfile; Structure RE my_record; Ptr_myfile = fopen ("test.bin", "rw"); For (i = 5; i> = 0; i--) {fseek (ptr_myfile, sizeof (structure RE) * i, SEEK_SET); Fred (& amp; my_record, sizeof (structure rec), 1, ptr_myfile); My_record.x = my_record.x + 100; FILIT (& amp; my_record, sizeof (structure REC), 1, ptr_myfile); } Fclose (ptr_myfile); Return; } Int main () {f_rite (); F_read (); F_rerite (); F_read (); Return 0; }
FOPIN has no "RW" flag you can read and write (update) To do this requires "r +" Since this is binary data, you should actually use "R + B", and "fb" and "rb" should be used in your f_rite function. Apart from this:
- Check the value of the call that can be unsuccessful, you may know that eg Fillibility failed.
- Your F_rerite functions repeat through 6 elements, you are away from one.
- Your f_rerite also writes the next element that you want to update the existing record. This means that you have to fade again after calling.
Comments
Post a Comment