Binary Mode operations in fopen
C programming language utilizes i/o library functions for handling file input and output operations and they are a part of the standard input and output library module. The standard input output header file stdio.h contains a group of file application programming interface library functions. Fopen is the function used for opening a file stream in C program and stdio.h should be included within the source file which contains the function prototype.
fopen function accepts the file path as a string and the mode of opening the file with one or two characters as a string. File can be in binary mode or text mode and we are mostly discussing the binary mode of operations in this article. Binary mode deals with data records and these records can be in the form of a single byte or several bytes. Most often structured objects are stored in binary mode and are stored sequentially.
Binary Mode fopen flags
|#1||Binary Read (rb)
Opens a file in binary read mode
|#2||Binary write (wb)
Opens a file in binary write mode.
|#3||Binary append (ab)
Opens a file in binary append mode i.e. to add at the end of file.
|#4||Binary read+write (r+b)
Opens preexisting file in read and write mode.
|#5||Binary write+read (w+b)
Open file for binary reading and writing also creates a new file if not exists.
|#6||Binary append+write (a+b)
Opens file in append mode i.e. data can be written at the end of file.
File opening error handing (errno & strerror)
fopen should return a FILE pointer once the file is opened successfully else a NULL value is returned indicating a failure. We can check error number errno to know the error code associated with the previous failure of fopen. We can check the file pointer for NULL condition and throw an error string containing the error number. This error number may not be understood by users so that can be translated to a error string using strerror() function.
A FILE pointer handle is returned by fopen, can be used for reading and writing file buffers from the application. C library has two API routines fread and fwrite. fread is to read records from the file and fwrite is to write records to a file.
A FILE pointer handle should be closed once it is no longer in use. We use fclose() library function to close the file pointer which was earlier opened using fopen(). fclose() works both for binary modes and text modes. A value of zero indicates the success of fclose() else fclose returns failures with a value of EOF.
Binary Mode fwrite
fwrite takes four arguments. First argument is the pointer where binary record is located. Next is the size of the record. Next to this is the number of records to write. Last argument is the file pointer. It returns the number of records written.
Binary Mode fwrite C code
== Student Database Add records == Name : Student 1 Roll : 1 Std : 1 Record added.
Binary Mode fread
fread takes identical arguments but the direction is read instead of write. First argument is the pointer where binary record is located. Next is the size of the record. Next to this is the number of records to write. Last argument is the file pointer. It returns the number of records read from the file.
Binary Mode fread C code
== Student Database Display records == Name : Student 1, Roll : 1, Std : 1
About our authors: Team EQA
You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Free alt softwares