Programming in C (Unit-V) New
Programming in C (Unit-V) New
5.1 Introduction
1. Text file
2. Binary file
1. Text Files
The user can create these files easily while handling files in C. It stores
information in the form of ASCII characters internally, and when the file is opened,
the content is readable by humans. It can be created by any text editor with a .txt
or .rtf (rich text) extension. Since text files are simple, they can be edited by any
text editor like Microsoft Word, Notepad, Apple Text Edit, etc.
2. Binary file
It stores information in the form of 0’s or 1’s, and it is saved with the .bin
extension, taking less space. Since it is stored in a binary number system
format, it is not readable by humans. Therefore, it is more secure than a text file.
5.2 File Operations
In C, you can perform four major operations on files, either text or binary:
FILE is basically a data type, and we need to create a pointer variable to work with
it (fptr). For now, this line is not important. It's just something you need when
working with files.
To actually open a file, use the fopen() function, which takes two parameters:
Paramete Description
r
filename The name of the actual file you want to open (or create),
like filename.txt
ptr = fopen("fileopen","mode");
For example,
fopen("E:\\cprogram\\newprogram.txt","w");
fopen("E:\\cprogram\\oldprogram.bin","rb");
● Let's suppose the file newprogram.txt doesn't exist in the location E:\cprogram. The
first function creates a new file named newprogram.txt and opens it for writing as
per the mode 'w'.
The writing mode allows you to create and edit (overwrite) the contents of the file.
● Now let's suppose the second binary file oldprogram.bin exists in the location E:\
cprogram. The second function opens the existing file for reading in binary
mode 'rb'. The reading mode only allows you to read the file, you cannot write into
the file.
Mode Description
#include <stdio.h>
#include <stdlib.h>
int main()
int num;
FILE *fptr;
fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
printf("Error!");
exit(1);
scanf("%d",&num);
fprintf(fptr,"%d",num);
fclose(fptr);
return 0;
#include <stdio.h>
int main()
int num;
FILE *fptr;
exit(1);
fscanf(fptr,"%d", &num);
fclose(fptr);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct threeNum
};
int main(){
int n;
FILE *fptr;
exit(1);
}
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fclose(fptr);
return 0;
#include <stdio.h>
#include <stdlib.h>
struct threeNum{
};
int main(){
int n;
struct threeNum num;
FILE *fptr;
exit(1);
fclose(fptr);
return 0;
This will waste a lot of memory and operation time. An easier way to get to the
required data can be achieved using fseek().
As the name suggests, fseek() seeks the cursor to the given record in the file.
Syntax of fseek()
Whence Meaning
Example 5: fseek()
#include <stdio.h>
#include <stdlib.h>
struct threeNum
};
int main()
int n;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","rb")) == NULL){
exit(1);
fclose(fptr);
return 0;
The following program open a random-access file, define a record format using a
struct, write data to the disk and close the file.
Function fseek sets the file position pointer to a specific position in the file, then
fwrite writes the data.
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "C:\\Turboc3\\BGI");
// Draw a line
line(100, 100, 200, 200);
// Draw a rectangle
rectangle(250, 150, 350, 250);
// Draw a circle
circle(500, 200, 50);
getch();
closegraph();
return 0;
}
In this example, we use the line, rectangle, and circle functions to draw basic shapes on
the graphics window.