File Handeling In C Programming

May application required that information be written to or read from an auxiliary (secondary) storage device. Such information is stored in disk in the form of data file. Thus, data file allows to store information permanently, and to access and retrieve(extract) information whenever necessary, this method is known as file handling in C.

Concept of Data File:

When program is processed the content of the variables are stored in RAM. If value of variable is needed in future, then this causes the problem because content of RAM gets erased when program is over or ended. To overcome this problem, data file are used to store data in secondary memory and retrieved whenever required.

Types of Files:

A file can be classified into two types based on the way the file stores the data. They are as follows:

  • Text Files
  • Binary Files

Text Files: A text file contains data in the form of ASCII characters and is generally used to store a stream of characters.

  • Each line in a text file ends with a new line character (‘\n’).
  • It can be read or written by any text editor.
  • They are generally stored with .txt file extension.
  • Text files can also be used to store the source code.

Binary Files: A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII characters. They contain data that is stored in a similar manner to how it is stored in the main memory.

  • The binary files can be created only from within a program and their contents can only be read by a program.
  • More secure as they are not easily readable.
  • They are generally stored with .bin file extension.

Note: – To point out the address of file or path of file, file pointer should be created as FILE *fptr .

            Where FILE is key word and *fptr is file pointer variable.

Mode of File Operations:

ModeDescription
rOpens a file for reading. The file must exist.
wOpens or creates a file for writing. If file exists, its contents are overwritten.
aOpens a file for appending. If file doesn’t exist, it’s created.
r+Opens a file for both reading and writing. The file must exist.
w+Opens a file for both reading and writing. It creates the file if it doesn’t exist.
a+Opens a file for both reading and appending. It creates the file if it doesn’t exist.
rbOpens a binary file in reading mode.
wbOpens or creates a binary file in writing mode.
abOpens a binary file in append mode.
rb+Opens a binary file for both reading and writing.
wb+Opens or creates a binary file for both reading and writing.
ab+Opens a binary file for both reading and appending.

Functions Used in file handling:

FunctionWhat it doesExample (FILE *fp)
fopen()Used for opening a new or existing file.fp=fopen(“C:\\temp\\file.txt”,”w”);
fprintf()Used to write data into a file.fprintf(fp,”%d %s”, num, str);
fscanf()Used to read data from a file.fscanf(fp, “%d %s”, num, str);
fclose()Closes a file.fclose(fp);
fputc()Used to write a character into a file. 
fgetc()Used for reading a character from a file. 
fseek()Used to set the file pointer to a given position. 
fputw()Used to write an integer to a file. 
fgetw()Used for reading an integer from a file. 
ftell()Used to return a current position.position= ftell(fp);
rewind()Used to set the file pointer at the beginning of a file. 
#include<stdio.h>
#include<conio.h>
main(){
    FILE *fptr;
    char name[20];
    int age;
    float salary;
    char ch='Y';
    /* open for writing */
    fptr = fopen("employee.txt", "w");
    if (fptr == NULL){
        printf("File does not exist.\n");
    }else{
    	while(ch=='Y'|ch=='y'){
			printf("Enter the name:\n");
			scanf("%s", &name);
			fprintf(fptr, "Name  = %s\n", name);
    		printf("Enter the age:\n");
    		scanf("%d", &age);
    		fprintf(fptr, "Age  = %d\n", age);
    		printf("Enter the salary:\n");
    		scanf("%f", &salary);
    		fprintf(fptr, "Salary  = %.2f\n\n", salary);
    		printf("Press Y to continuee or any key to exit: ");
    		scanf("%s",&ch);
		}	
	}
    fclose(fptr);
    getch();
}
#include <stdio.h>
#include<conio.h>
main(){
	FILE *fptr;
	char ch;
	char name[30];
	int age;
	float salary;
	fptr = fopen("employee1.txt", "r");
	if (NULL == fptr) {
		printf("file can't be opened \n");
	}else{
		printf("Content of this file are: \n\n");
	 	while (fscanf(fptr,"%s%d%f",&name,&age,&salary) != EOF){
			if(salary>50000){
				printf("Name: %s Age: %d Salary: %.2f\n",name,age,salary);
			}
		}
	}
	fclose(fptr);
	getch();
}
#include <stdio.h>
#include<conio.h>
#include<string.h>
typedef struct{
	char name[30];
	int age;
	float salary;
}staff;
main(){
	FILE *fptr;
	int i=0, n=3;
	staff e1[n],temp;
	fptr = fopen("employee.txt", "r");
	if (NULL == fptr) {
		printf("file can't be opened \n");
	}else{
		printf("Content of this file are: \n\n");
	 	while (fscanf(fptr,"%s%d%f",&e1[i].name,&e1[i].age,&e1[i].salary) != EOF){
			  printf("Name: %s Age: %d Salary: %.2f\n",e1[i].name,e1[i].age,e1[i].salary);
			  i++;
		}
	for(i=0;i<n-1;i++){
  		for(int j=i+1;j< n;j++){
   			if(strcmp(e1[i].name, e1[j].name) > 0){
    			temp = e1[i];
    			e1[i] = e1[j];
    			e1[j] = temp;
   			}
  		}
 	}
 	printf("\n Records in Alphabetical order are:\n");
 	for(i=0;i<n;i++){
  		printf("Name:\t %s\n", e1[i].name);
  		printf("Salary:\t %.2f\n", e1[i].salary);
  		printf("Age: \t %d\n\n", e1[i].age);
 	}
	}
	fclose(fptr);
	getch();
}

Leave a Reply