FILES HANDLING Examples



C programming code to open a file and to print it contents on screen.
#include<stdio.h>
#include<stdlib.h>
 
main()
{
   char ch, file_name[25];
   FILE *fp;
 
   printf("Enter the name of file you wish to see ");
   gets(file_name);
 
   fp = fopen(file_name,"r"); // read mode
 
   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }
 
   printf("The contents of %s file are :- \n\n", file_name);
 
   while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);
 
   fclose(fp);
   return 0;
}
Read file program executable.

Output of program:
read file

c program to copy files

C program to copy files: This program copies a file, firstly you will specify the file to copy and then you will enter the name of target file, You will have to mention the extension of file also. We will open the file that we wish to copy in read mode and target file in write mode.

C programming code

#include <stdio.h>
#include <stdlib.h>
 
main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;
 
   printf("Enter name of file to copy\n");
   gets(source_file);
 
   source = fopen(source_file, "r");
 
   if( source == NULL )
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
 
   printf("Enter name of target file\n");
   gets(target_file);
 
   target = fopen(target_file, "w");
 
   if( target == NULL )
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
 
   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);
 
   printf("File copied successfully.\n");
 
   fclose(source);
   fclose(target);
 
   return 0;
}
File copy program executable.
Output of the above program is shown in image below :-

copy file

c program to merge two files

This c program merges two files and store their contents in an another file. The files which are to be merged are opened in read mode and the file which contains content of both the files is opened in write mode. To merge two files first we open a file and read it character by character and store the read contents in another file then we read the contents of another file and store it in file, we read two files until EOF ( end of file ) is reached.

C programming code

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
 
main()
{
   FILE *fs1, *fs2, *ft;
 
   char ch, file1[20], file2[20], file3[20];
 
   printf("Enter name of first file ");
   gets(file1);
 
   printf("Enter name of second file ");
   gets(file2);
 
   printf("Enter name of file which will store contents of two files ");
   gets(file3);
 
   fs1 = fopen(file1,"r");
   fs2 = fopen(file2,"r");
 
   if( fs1 == NULL || fs2 == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      getch();
      exit(EXIT_FAILURE);
   }
 
   ft = fopen(file3,"w");
 
   if( ft == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }
 
   while( ( ch = fgetc(fs1) ) != EOF )
      fputc(ch,ft);
 
   while( ( ch = fgetc(fs2) ) != EOF )
      fputc(ch,ft);
 
   printf("Two files were merged into %s file successfully.\n",file3);
 
   fclose(fs1);
   fclose(fs2);
   fclose(ft);
 
   getch();
   return 0;
}
Click here to download executable file of the above program.
Output of above program is shown in image below :-

merge files

c program to list files in directory

This program list all files present in a directory/folder in which this executable file is present. For example if this executable file is present in C:\\TC\\BIN then it will lists all the files present in C:\\TC\\BIN.

C programming code

#include<stdio.h>
#include<conio.h>
#include<dir.h>
 
main()
{
   int done;
   struct ffblk a;
 
   printf("Press any key to view the files in the current directory\n");
 
   getch();
 
   done = findfirst("*.*",&a,0);
 
   while(!done)
   {
      printf("%s\n",a.ff_name);
      done = findnext(&a);
   }
 
   getch();
   return 0;
}
Download list files program executable.
Output c program to list files is shown in image below :-

list files

c program to delete a file

This c program deletes a file which is entered by the user, the file to be deleted should be present in the directory in which the executable file of this program is present. Extension of the file should also be entered, also note that deleted file doesn't go to recycle bin, remove macro is used to delete the file. If there is an error in deleting the file then an error will be displayed using perror function.

C programming code

#include<stdio.h>
 
main()
{
   int status;
   char file_name[25];
 
   printf("Enter the name of file you wish to delete\n");
   gets(file_name);
 
   status = remove(file_name);
 
   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }
 
   return 0;
}
Delete file program executable.
Output of program:
output of c program to delete file


No comments: