Wednesday, 23 April 2014

30. Write a program that prompts the user the name of a file and then counts and displays the number of bytes in the file. And create a duplicate file with the word ’.backup’ appended to the file name. Please check whether file was successfully opened, and display an error message, if not.



#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<string.h>
void main()
{
          int IntFileSize=0,i;
          char chUserFile[20],c;
          FILE*F1User,*F1Bak;
          clrscr();
          printf("\n Please insert Filename & Extansion -> ");
          scanf("%s", chUserFile);

          F1User=fopen(chUserFile,"r");
          if(F1User==NULL)
          {
                   printf("File does not exist or File I/O Error");
                   getch();
                   exit(0);
          }
          strcat(chUserFile,".backup");
          F1Bak=fopen(chUserFile,"w");
          if(F1User==NULL)
          {
                   printf("File I/O Error!");
                   getch();
                   exit(0);
          }
          while((c=getc(F1User))!=EOF)
          {
                   putc(c,F1Bak);
                   IntFileSize++;
          }

          printf("\n file is %d bytes", IntFileSize);
          fclose(F1User);
          fclose(F1Bak);
          getch();
}

No comments:

Post a Comment