Arduino Projects    |   Raspberry Pi  |     Electronic Circuits   |   Electronics Reference Design Library  |   AVR    |    PIC    |    8051    |    Electronic Projects

Programs related to file handling in C language.

2 posts / 0 new
Last post
Monika
Monika Nishant's picture
Offline
Joined: 21/02/2013
Posts: 3
Points: 5
Programs related to file handling in C language.

Hi!!

     Its great to be a part of this team. I just went through the C programs disscussed over here and found them quite worthy. Can I please get some programs related to file handling in C.

 

 

Arjun Vaghani
arjunvaghani1009's picture
Offline
Joined: 24/03/2013
Posts: 2
Points: 10

 

/*simple program of file Handling*/
/*copy content of 1 file to another*/
 
#include<stdio.h>
main(int argc, char **argv)
{
 char ch;
 FILE *fp1,*fp2;
 
 if(argc!=3)
 {
   printf("Invalid syntax: use: ./cpy <source_filename> <destinatin_filename>\n");
   return;
 }
 
 fp1=fopen(argv[1],"r"); 
 if(fp1==NULL)
 {
 printf("source file does not exists\n");
 return;
 }
fclose(fp1);
 
 fp2=fopen(argv[2],"r");
 if(fp2!=NULL)
 {
here: printf("Ur file already exists..Do u want to overwrite? press y for yes and n for no:");
      scanf("%c",&ch);
 
      if(ch=='n'||ch=='N')
         return;
      else if(ch!='y'&& ch!='Y')
      {
         printf("Invalid choice\n");
         goto here;
      }
  fclose(fp2);
  while((ch=getchar())!='\n'); /*for clearing buffer*/
 }
 
 fp1=fopen(argv[1],"r");
 fp2=fopen(argv[2],"w");
 
 while((ch=fgetc(fp1))!=EOF)
 {
  fputc(ch,fp2);
 }
 fclose(fp1);
 fclose(fp2);
}
 

 

Login or register to post comments