Programming in C—Binary I/O Functions.steemCreated with Sketch.

Hello Everyone

images.jpeg

Image Source

I hope you all are fine and safe inside your homes. This is a weird time ongoing in the whole world and I hope it will get over soon. As during this time, everyone is locked into their homes. I want to share C programming language with you. I hope you guys like it, so lets happen today's topic.

Binary File Input Output (I/O) Functions

Have you ever thought, how can a large amount of numerical data be stored in file? Is it sufficient to store in text mode?
Well, a large amount of numerical data can not be stored in text mode. In such a case Binary File is used.
Working on binary files is identical to text file with rare discrepancies in the opening, reading and writing to file.
Opening modes of binary files are rb+, rb, wb+, wb, ab+ and ab. Only differences between opening modes of binary files and text files is that b is appended to indicate it is a *Binary File.

fread() and fwrite()— Functions for reading and writing Binary File.

These two functions fread() and fwrite() are used for reading from and writing to a file on disk respective of binary files.
fwrite()
Functions fwrite() takes 4 arguments. Address, size of data should be written in disk, number of type of data and pointer to file where user wants to write.
Syntax:
fwrite(address_data,size_data,numbers_data,pointer_to_file);
Example:
#include <stdio.h>
struct marks
{
int m1, m2,m3,m4,m5;
};
void main()
{
int n;
struct marks m;
FILE *fptr;
if ((fptr = fopen("C:\TURBOC3\mark.bin","wb")) == NULL){
printf("File Cannot Open!");
exit(1);
}
printf("Enter 5 Students Marks\n");
for(n = 1; n <= 5; ++n)
{
printf("Enter English Mark of Student %d : ", n);
scanf("%d",&m.m1);
printf("Enter Math's Mark of Student %d : ", n);
scanf("%d",&m.m2);
printf("Enter Physics Mark of Student %d : ", n);
scanf("%d",&m.m3);
printf("Enter Chemistry Mark of Student %d : ", n);
scanf("%d",&m.m4);
printf("Enter Python Mark of Student %d : ", n);
scanf("%d",&m.m5);
fwrite(&m, sizeof(struct marks), 1, fptr);
}
fclose(fptr);
}

Output of above program:

IMG_20200430_190828.jpg

Output got after execution of program

In the above program, user can create a new file mark.bin in C:\TURBOC3\ path. Structure of the marks with 5 integers are declared as m1,m2,m3,m4 and m5 and are defined it in new function m as main function.
User can read 5 subject marks and can store value into the file with the help of fwrite() function. Address of the m is stored in first parameter and second parameter takes size of marks structured.
Since, only one instance of m is to be inserted so third parameter is 1. *fptr points to the file where data is to be stored.
On last step, file is closed.
fread()
The function fread() also takes the same 4 arguments which fwrite() takes.
Syntax:
fread(address_data,size_data,numbers_data,pointer_to_file);
Example:
#include <stdio.h>
struct marks
{
int m1, m2,m3,m4,m5;
};
void main()
{
int n;
struct marks m;
FILE *fptr;
if ((fptr = fopen("C:\TURBOC3\mark.bin","rb")) == NULL){
printf("Cannot Open File !");
exit(1);
}
printf("Marks are\n");
for(n = 1; n <= 5; ++n)
{
fread(&m, sizeof(struct marks), 1, fptr);
printf("Student %d Marks : English: %d\t Maths : %d\t Physics: %d\t Chemistry : %d\t
Python: %d\n",n, m.m1, m.m2, m.m3,m.m4,m.m5);
}
fclose(fptr);
}

Output of the above program:

IMG_20200430_203542.jpg

Output got after execution of above program

In the above program, user can read the same file mark.bin in C:\TURBOC3\ path and loop through records respectively one by one. Simply, one marks record of marks size is read from the file indicated *fptr into structure m. By doing all this procedure, user will get the same records which inserted in previous Example.

Logo.png

My last posts on programming, if you want to read go through it.


1: Ist post—File processing.
2: 2nd post—File Operations
3: 3rd post—Text File I/O

Logo.png

Thank you.

I hope you guys liked my post.

Keep Supporting.

STAY TUNED FOR NEXT POST

UPVOTECOMMENTRESTEEM
IF YOULIKEDMY POST

Logo.png

Created by @zord189

Stay Home, Stay Safe

@peerzadazeeshan

Sort:  

This post has been rewarded by the Steem Community Curation Project. #communitycuration06.

As a follower of @followforupvotes this post has been randomly selected and upvoted! Enjoy your upvote and have a great day!

@peerzadazeeshan currently c language is used everywhere as it is master of all language. before i also tried to learn but cannot get it.

Few years ago it was considered as master of all languages but from past 3-4 years Java was considered as master language.
Thanks to corona, it is used everywhere nowadays for researching and now it surpassed Java too...

Java is pretty much ancient history, but instead JavaScript is used extensively by web-enabled applications... A lot of programs recently have been written in Python.

Assignment inside if condition might confuse people still new to programming.

Coin Marketplace

STEEM 0.18
TRX 0.16
JST 0.030
BTC 63017.22
ETH 2457.38
USDT 1.00
SBD 2.61