Computer Fundamentals with C and Unix
(Binary Files)
Lecture 12: Jan. 18, 2023 Prof. K.R. Chowdhary : Professor of CS
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.
The purpose of the fprintf() function is to write formatted data to a file. The fprintf() function is almost identical to the printf() function. The only difference is that the fprintf() function writes data into the given file.
Moreover, an extra argument is also present in the fprintf() function. In the fprintf() function, the file pointer points to the file where the formatted output will be written, and if the write is successful, it will print the total count of characters. In comparison, the purpose of the fscanf() function is to read formatted data from a file. The fscanf() function reads the stream in the form of byte, interprets the input according to the format, and stores the format into their argument for the output. It reads from a file that also contains a pointer, i.e., file pointer, so it reads a specific area or part of the file instead of the whole stream.
fscanf() function of the C programming language reads a specific part of the file instead of reading the whole stream. For this process, the fscanf() function uses a file pointer. This function works on two parameters: streams and formats. This stream is the file’s pointer, and the format contains a list of placeholders used to read the specific type of data.
Syntax of fprintf() and fscanf() function is:
int fprintf(FILE *stream, const char *format, …) int fscanf(FILE *stream, const char *format, …)
In both functions’ above-given syntax, input passes by two parameters. Those two parameters are as follows:
Stream: A stream is a pointer to the file that indicates the file object which recognizes the file stream.
Format: It is the parameter that tells about how to format the data according to the user’s requirements or to make the data easy to read.
/* blockiow.c */ #include <stdio.h> #iclude <stdlib.h> main(){ FILE *fp; fp = fopen("mydoc.txt", "w"); fprintf(fp, "Hello, I am K R Chowdhary.\n"); fprintf(fp, "This c and Unix Class.\n"); fclose(fp); }
/* blockior.c */ #include <stdio.h> #include <stdlib.h> int main(){ FILE *fp; char cbuff[255]; //char buffer to store data of one read of file fp = fopen("mydoc.txt", "r"); while(fscanf(fp, "%s", cbuff)!=EOF){ printf("%s ", cbuff); } fclose(fp); }
The fprintf() and the fscanf() functions are standard inbuilt functions of the C programming language used in file handling. The fprintf() function returns a numerical value, the number of printed strings, whereas the fscanf() function returns the number of fields converted successfully.
Functions fread() and fwrite() are used for reading from and writing to a file on the disk
respectively in case of binary files.
The fwrite() for Writing to a binary file requires its parameters as:
original address of data to be written in the disk
size of data to be written in the disk
number of such type of data
pointer to the file where you want to write.
For writing a binary file we use following built-in function call:
fwrite(addressData, sizeData, numbersData, pointerToFile);
For reading a binary file, we use following command:
fread(addressData, sizeData, numbersData, pointerToFile);
The program in Fig. 12.3 shows the output of a program that has read a binary file and the data read have been printed on the screen.