Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Programming in C Base 64 Encode File Write a program that accepts two command li

ID: 3861562 • Letter: P

Question

Programming in C Base 64 Encode File

Write a program that accepts two command line arguments. The first is the name of a file that
is to be used as the input file and the second is the name of the output file.


The program is to base-64 encode the first file, which is to be treated as a binary file, into the
second. The first line of the output file, which is a text file, should contain the name of the first
file (so that the reconstructed data can be placed in a file of the correct name later).Your program should print to the console the total number of bytes contained in the input file.

base-64.c

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

char map(unsigned value)
{
   if (value < 26) return 'A' + value;
   if (value < 52) return 'a' + value - 26;
   if (value < 62) return '0' + value - 52;
   if (62 == value) return '+';
   if (63 == value) return '/';
      
   return '?';
}

unsigned unmap(char c)
{
   if ('/' == c)   return 63;
   if ('+' == c)   return 62;
   if (isdigit(c)) return 52 + (c - '0');
   if (islower(c)) return 26 + (c - 'a');
   if (isupper(c)) return 0 + (c - 'A');
      
  
   return 0xFFFFFFFF;
}

int main(void)
{
   char *message = "Hello World!";
  
   unsigned data = 062434752; // 0xCA35EA;
   char chars[4];
  
   printf("DATA: %o ", data);
  
   int i;
   for (i = 0; i < 4; i++)
   {
       unsigned value = (data >> (18 - 6 * i)) & 077;
      
       chars[i] = map(value);
   }
  
   printf("MSG : ");
   int i4;
   for (i4 = 0; i4 < 4; i4++)
   {
      
       printf("%c", chars[i4]);
   }
   printf(" ");
  
   data = 0;
   int i2;
   for (i2 = 0; i2 < 4; i2++)
   {
       unsigned value = unmap(chars[i2]);
      
       data <<= 6;
       data += value;
   }
  
   printf("DATA: %o ", data);

   return EXIT_SUCCESS;
}

Explanation / Answer

Program that accepts two command line arguments:

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>

int main(int argv,char* argc[])
{
   FILE ptr1,ptr2;
   ptr1 = fopen(argc[1],"rb")
   ptr2 = fopen(argc[2],"w")
  
   // Writing the input file name as the first line of output file.
   fprintf(ptr2,"%s",argc[1]);
  
   struct stat sb;
   char filename[] = argc[1];
  
   // Writing the total no. of bytes of file1 to file 2.
   fprintf(ptr2,"%lld bytes ",(long long) sb.st_size);
  
   return 0;
}