Reading a File in Binary Into Buffer
In an earlier tutorial nosotros talked near file I/O functions and the use of text files. In this C programming tutorial nosotros are going to talk about the employ of binary files.
Binary files
Binary files are very like to arrays of structures, except the structures are in a deejay-file rather than an assortment in memory. Binary files take two features that distinguish them from text files:
- You lot can instantly employ any structure in the file.
- You tin can change the contents of a structure anywhere in the file.
Subsequently you lot have opened the binary file, you can read and write a structure 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. Subsequently reading the structure the pointer is moved to point at the adjacent structure.
A write performance will write to the currently pointed-to structure. Afterwards the write functioning the file position indicator is moved to point at the adjacent structure.
The fseek function will motion the file position indicator to the tape that is requested.
Remember that you go on track of things, because the file position indicator tin not only point at the beginning of a construction, but can also bespeak to any byte in the file.
The fread and fwrite role 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),one,ptr_myfile); This fread statement says to read ten bytes (size of rec) from the file ptr_myfile into memory accost &my_record. But one block is requested. Changing the one into ten will read in x blocks of x bytes at once.
Let's look at a write case:
#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","wb"); if (!ptr_myfile) { printf("Unable to open file!"); return 1; } for ( counter=i; counter <= 10; counter++) { my_record.x= counter; fwrite(&my_record, sizeof(struct rec), i, 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 main function nosotros open (fopen) a file for writing (w). Then we cheque if the file is open, if not, an error bulletin is displayed and we leave the program. In the "for loop" nosotros fill the construction member x with a number. Then nosotros write the record to the file. Nosotros do this x times, thus creating 10 records. Afterward writing the ten records, nosotros will shut the file (don't forget this).
So at present we have written to a file, permit's read from the file nosotros take simply created. Take a await at the example:
#include<stdio.h> /* Our structure */ struct rec { int x,y,z; }; int chief() { 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; } for ( counter=ane; 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 2 lines that are changed are the two lines in the "for loop". With the fread we read-in the records (one by i). After we have read the tape nosotros print the fellow member x (of that record).
The but thing we need to explicate is the fseek pick. The function fseek must be declared like this:
int fseek(FILE * stream, long int showtime, int whence); The fseek role sets the file position indicator for the stream pointed to by the stream. The new position, measured in characters from the beginning of the file, is obtained by adding offset to the position specified past whence. Three macros are alleged in stdio.h called: SEEK_SET, SEEK_CUR and SEEK_END.
If the position declared by whence is SEEK_SET, then the position is the beginning of the file.
The SEEK_END can be used if y'all want to go to the end of the file. (Using negative numbers it is possible to move from the end of the file.)
If whence is SEEK_CUR then the position is prepare, x bytes, from the current position.
Permit's take a look at an example:
#include<stdio.h> /* Our structure */ struct rec { int x,y,z; }; int primary() { int counter; FILE *ptr_myfile; struct rec my_record; ptr_myfile=fopen("test.bin","rb"); if (!ptr_myfile) { printf("Unable to open file!"); return one; } 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\n",my_record.ten); } fclose(ptr_myfile); render 0; } In this example we are using fseek to seek the concluding tape in the file. This tape nosotros read with fread statement and with the printf statement nosotros print member 10 of the construction my_record. As you lot tin can see the "for loop" also changed. The "for loop" will now inaugural to zilch. 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 reverse social club.
A last note: if you set the file position indicator to a position in a file and you want the starting time position in a file then you lot can use the function rewind to the starting time position in the file. The function rewind can be used like this:
#include<stdio.h> /* Our structure */ struct rec { int 10,y,z; }; int master() { 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=one; counter <= 10; counter++) { fread(&my_record,sizeof(struct rec),1,ptr_myfile); printf("%d\n",my_record.x); } fclose(ptr_myfile); return 0; } With the fseek argument in this case we become to the cease of the file. So we rewind to first position in the file. Then read-in all records and impress the value of member x. Without the rewind you will go garbage. (Endeavor it!)
That is all for this tutorial.
This entry was posted in C Tutorials. You tin follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.
Tweet This! or use to share this post with others.
Source: https://www.codingunit.com/c-tutorial-binary-file-io
Postar um comentário for "Reading a File in Binary Into Buffer"