Write a program to read a binary file and write a binary fil

Write a program to read a binary file, and write a binary file. Write out the characters read, replacing any unprintable characters with a question mark.

Your program should process the parameter list. The first argument should be the input file, and the second one should be the output file. If the user passes \"-h\" to your program, write out a \"help\" message talking about the program, and quit. If the user does not specify exactly 2 filenames, also write out the help message and quit. Make sure to check that the input file exists.

For the purpose of this assignment, we consider characters 32 to 127 to be printable. Also, the line-feed character is printable. All other characters should be considered unprintable.

Solution


#include <stdio.h>

int main(int argc, char **argv){
   if(argc >= 0){
       int count = 0;
       char ch;
       FILE *fp = fopen(argv[1], \"rb\");
       FILE *wfp = fopen(argv[2], \"wb\");
       if(fp){
           while(1){
               fseek(fp, count, SEEK_SET);
               if(fread(&ch, 1, 1, fp) < 1)
                   break;
               if(ch < 32 || ch > 127)
                   ch = \'?\';
               fwrite(&ch, 1, 1, wfp);
               ++count;
           }
           fclose(wfp);
           fclose(fp);
       }
   }
   else{
       // handle help and invalid condition here
   }
   return 0;
}

Write a program to read a binary file, and write a binary file. Write out the characters read, replacing any unprintable characters with a question mark. Your p

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site