C Programming : File Handling (BIM-TU)

By Ashesh Neupane

In this article we have shown some examples like copying contents from one file to another, separating odd and even numbers to a separate file, storing text into file , searching the data from the file and so on.

1. Write a program to copy the content of file “tu.txt” into “university.txt”.

#include<stdio.h>
#include<string.h>
int main()
{
	FILE *ptr1=fopen("tu.txt","r");
	FILE *ptr2=fopen("university.txt","w");
	char a[100];
	if(ptr1==NULL || ptr2==NULL)
		printf("Unable to open file");
	else
	{
		while(fgets(a, sizeof(a), ptr1) != NULL)
		{
			fprintf(ptr2,"%s",a);
		}
		fclose(ptr1);
		fclose(ptr2);
		printf("Content Copied Successfully");
	}
	return 0;
}
Content Copied Successfully

2. A data file “number.txt” contains some numbers. WAP to read the numbers and store all even numbers into “even.txt” and all odd numbers into “odd.txt”.

#include<stdio.h>
int main()
{
	FILE *ptr1=fopen("number.txt","r");
	FILE *ptr2=fopen("even.txt","w");
	FILE *ptr3=fopen("odd.txt","w");
	int num;
	if(ptr1==NULL || ptr2==NULL || ptr3==NULL)
	printf("Unable to open file");
	else
	{
		while(!feof(ptr1))
		{
			fscanf(ptr1,"%d",&num);
			if(num%2==0)
			fprintf(ptr2,"%d",num);
			else
			fprintf(ptr3,"%d",num);
		}
		fclose(ptr1);
		fclose(ptr2);
		fclose(ptr3);
		printf("Task Completed Successfully");
	}
	return 0;
} 
Task Completed Successfully

3. Write a program to input a line of text, store it in a file and then read from file and display its contents.

#include<stdio.h>
int main()
{
	char a;
	char b[100];
	FILE *ptr1=fopen("abc.txt","w");
	if(ptr1==NULL)
	printf("Unable to open file");
	else
	{
		printf("Enter a line of text:");
		gets(b);
		fputs(b,ptr1);
		fclose(ptr1);
	}
	FILE *ptr=fopen("abc.txt","r");
	if(ptr==NULL)
	printf("Unable to open file");
	else
	{
		while(!feof(ptr))
		{
			a=fgetc(ptr);
			printf("%c",a);
		}
		fclose(ptr);
	}
	return 0;
}
Enter a line of text:My name is Ashesh Neupane
My name is Ashesh Neupane

4. A data file contains name,address,salary of some person. WAP to input any address and show the records matching that address.

#include<stdio.h>
#include<string.h>
int main()
{
	FILE *ptr=fopen("record.txt","r");
	char name[20],address[20],address1[20];
	int salary;
	if(ptr==NULL)
	printf("Unable to open file");
	else
	{
		printf("Enter address to search:");
		scanf("%s",&address1);
		while(!feof(ptr))
		{
			fscanf(ptr, "%s %s %d\n",&name,&address,&salary);
			if(strcmpi(address,address1)==0)
			printf("%s %s %d \n",name,address,salary);
		}
		fclose(ptr);
	}
	return 0;
}

5. A data file contains name,address,salary of some person. WAP to increase salary of all persons by 15%.

#include<stdio.h>
int main()
{
	FILE *ptr=fopen("record.txt","r");
	FILE *ptr1=fopen("temp.txt","w");
	char name[20],address[20];
	int salary,newsalary;
	if(ptr==NULL || ptr1==NULL)
	printf("Unable to open file");
	else
	{
		while(!feof(ptr))
		{
			fscanf(ptr,"%s %s %d\n",&name,&address,&salary);
			newsalary=salary+(0.15*salary);
			fprintf(ptr1,"%s %s %d\n",name,address,newsalary);
		}
		fclose(ptr);
		fclose(ptr1);
		remove("record.txt");
		rename("temp.txt","record.txt");
	}
	return 0;
}

6.

Create a menu driven program to store and display records (name, address, salary of a person)

   1. Add Record

   2. Display Record

   Enter your choice:

#include<stdio.h>
#include<process.h>
#include<string.h>
void addrecord();
void displayrecord();
int main()
{
    int choice;
    for(;;)
    {
    	printf("\n\n");
    	printf("1.Add Record\n2.Display Record\n");
    	printf("Enter your choice: ");
    	scanf("%d",&choice);
    	switch(choice)
    	{
        	case 1:addrecord();
        	break;
        	case 2:displayrecord();
        	break;
        	default: printf("Invalid choice:"); return 0;
    	}
    }
}
void addrecord()
{
   char name[20],address[20];
   int salary;
   FILE *ptr=fopen("person.txt","a");
   if(ptr==NULL)
   {
       printf("Unable to open file");
   }
   else
   {
       printf("Enter name,address,salary: ");
       scanf("%s%s%d",&name,&address,&salary);
       fprintf(ptr,"%s %s %d\n",name,address,salary);
       fclose(ptr);
   }
}

void displayrecord()
{
   char name[20],address[20];
   int salary;
   FILE *ptr=fopen("person.txt","r");
   if(ptr==NULL)
   {
       printf("Unable to open file");
   }
   else
   {
       while(!feof(ptr))
       {
         fscanf(ptr,"%s %s %d\n",&name,&address,&salary);
         printf("%s %s %d\n",name,address,salary);
       }

       fclose(ptr);
   }
}

Ashesh Neupane is the Co-Founder and Former Admin of HighApproach. He is also a student of Bachelor of Information Management (BIM) at Tribhuvan University.

2 thoughts on “C Programming : File Handling (BIM-TU)”

Leave a Comment

Slide to prove you're not a bot/spammer *