Best Way to Read a Binary String to Add Them
In an earlier tutorial we talked about file I/O functions and the employ of text files. In this C programming tutorial nosotros are going to talk about the use of binary files.
Binary files
Binary files are very similar to arrays of structures, except the structures are in a deejay-file rather than an array in memory. Binary files have ii features that distinguish them from text files:
- You lot tin instantly utilise any structure in the file.
- Y'all tin can change the contents of a structure anywhere in the file.
After yous accept opened the binary file, you can read and write a construction or seek a specific position in the file. A file position indicator points to record 0 when the file is opened.
A read operation reads the structure where the file position indicator is pointing to. Afterward reading the structure the pointer is moved to signal at the side by side structure.
A write operation volition write to the currently pointed-to construction. Later on the write operation the file position indicator is moved to point at the side by side structure.
The fseek role will move the file position indicator to the record that is requested.
Remember that you go on track of things, because the file position indicator tin can not only bespeak at the beginning of a structure, just can also point to any byte in the file.
The fread and fwrite function takes four parameters:
- A memory address
- Number of bytes to read per block
- Number of blocks to read
- A file variable
For example:
fread(&my_record,sizeof(struct rec),1,ptr_myfile);
This fread statement says to read x bytes (size of rec) from the file ptr_myfile into memory address &my_record. Only one block is requested. Changing the i into x will read in 10 blocks of 10 bytes at once.
Permit'south wait at a write example:
#include<stdio.h> /* Our structure */ struct rec { int 10,y,z; }; int main() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("test.bin","wb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } for ( counter=1; counter <= x; counter++) { my_record.10= counter; fwrite(&my_record, sizeof(struct rec), 1, ptr_myfile); } fclose(ptr_myfile); return 0; }
In this instance we declare a structure rec with the members x,y and z of the type integer. In the master function we open (fopen) a file for writing (w). Then we bank check if the file is open, if non, an fault message is displayed and we get out the programme. In the "for loop" we fill up the structure member x with a number. Then nosotros write the record to the file. We do this ten times, thus creating ten records. After writing the ten records, we will shut the file (don't forget this).
So at present we have written to a file, let's read from the file we have but created. Take a look at the instance:
#include<stdio.h> /* Our structure */ struct rec { int 10,y,z; }; int principal() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("exam.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return i; } for ( counter=1; counter <= 10; counter++) { fread(&my_record,sizeof(struct rec),1,ptr_myfile); printf("%d\n",my_record.x); } fclose(ptr_myfile); return 0; }
The only two lines that are changed are the two lines in the "for loop". With the fread we read-in the records (one by one). After we have read the record nosotros print the fellow member x (of that record).
The only thing we need to explain is the fseek option. The role fseek must be declared similar this:
int fseek(FILE * stream, long int offset, int whence);
The fseek office sets the file position indicator for the stream pointed to by the stream. The new position, measured in characters from the showtime of the file, is obtained by adding get-go to the position specified past whence. Three macros are declared in stdio.h chosen: SEEK_SET, SEEK_CUR and SEEK_END.
If the position declared past whence is SEEK_SET, then the position is the commencement of the file.
The SEEK_END tin be used if you want to go to the terminate of the file. (Using negative numbers it is possible to movement from the terminate of the file.)
If whence is SEEK_CUR and so the position is set, ten bytes, from the current position.
Let's take a look at an example:
#include<stdio.h> /* Our structure */ struct rec { int x,y,z; }; int principal() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("exam.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } for ( counter=9; counter >= 0; counter--) { fseek(ptr_myfile,sizeof(struct rec)*counter,SEEK_SET); fread(&my_record,sizeof(struct rec),1,ptr_myfile); printf("%d\northward",my_record.x); } fclose(ptr_myfile); return 0; }
In this example nosotros are using fseek to seek the terminal record in the file. This record we read with fread statement and with the printf argument we print member x of the structure my_record. As y'all can run across the "for loop" also changed. The "for loop" volition now countdown to nada. This counter is then used in the fseek statement to set the file pointer at the desired record. The result is that we read-in the records in the opposite order.
A concluding note: if you set the file position indicator to a position in a file and yous want the first position in a file then you can use the function rewind to the first position in the file. The function rewind tin be used like this:
#include<stdio.h> /* Our construction */ struct rec { int x,y,z; }; int principal() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("test.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } fseek(ptr_myfile, sizeof(struct rec), SEEK_END); rewind(ptr_myfile); for ( counter=1; counter <= 10; counter++) { fread(&my_record,sizeof(struct rec),1,ptr_myfile); printf("%d\due north",my_record.x); } fclose(ptr_myfile); return 0; }
With the fseek argument in this example we get to the stop of the file. So nosotros rewind to beginning position in the file. And so read-in all records and print the value of member x. Without the rewind you volition get garbage. (Effort it!)
That is all for this tutorial.
This entry was posted in C Tutorials. Yous can follow whatsoever responses to this entry through the RSS 2.0 feed. Both comments and pings are currently airtight. Tweet This! or utilize to share this post with others.
Source: https://www.codingunit.com/c-tutorial-binary-file-io
0 Response to "Best Way to Read a Binary String to Add Them"
Post a Comment