Write a program that accepts two command line arguments. The first is the name o
ID: 3862187 • Letter: W
Question
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.
This is what i have. it take in a file and read it and then print same information onto another file, name does not mater.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int BUFFER_SIZE = 3;
FILE *source;
FILE *destination;
int n;
int count = 0;
int written = 0;
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 fname[128];
printf("Enter .txt file name to read ");
scanf("%s",fname);
strcat(fname,".txt");
source = fopen(fname,"rb");
char outname[128];
printf("Enter .txt file name to print ");
scanf("%s",outname);
strcat(outname,".txt");
destination = fopen(outname,"wb");
unsigned char buffer[BUFFER_SIZE];
unsigned data;
int i = 0;
if (source) {
while (!feof(source)) {
n = fread(buffer, 1, BUFFER_SIZE, source);
count += n;
data =buffer[0];
// printf("MSG : ");
for (int i = 0; i < 3; i++)
{
//
// buffer[i] = map(buffer[i]);
printf(" %d", buffer[i]);
}
// printf("The contents of buffer[] are %s ", buffer);
fwrite(buffer, 1, n, destination);
}
printf(" %d bytes read from library. ", count);
} else {
printf("fail ");
}
fclose(source);
fclose(destination);
return EXIT_SUCCESS;
}
in the file i have 062434752
are teacher gave us
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.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);
for (int i = 0; i < 4; i++)
{
unsigned value = (data >> (18 - 6 * i)) & 077;
chars[i] = map(value);
}
printf("MSG : ");
for (int i = 0; i < 4; i++)
{
printf("%c", chars[i]);
}
printf(" ");
data = 0;
for (int i = 0; i < 4; i++)
{
unsigned value = unmap(chars[i]);
data <<= 6;
data += value;
}
printf("DATA: %o ", data);
return EXIT_SUCCESS;
}
which print
DATA: 62434752
MSG : yjnq
DATA: 62434752
--------------------------------
Process exited after 0.07087 seconds with return value 0
Press any key to continue . . .
the input file is the same but i can not seem to get the output file to print yjnq
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int BUFFER_SIZE = 3;
FILE *source;
FILE *destination;
int n;
int count = 0;
int written = 0;
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 fname[128];
printf("Enter .txt file name to read ");
scanf("%s",fname);
strcat(fname,".txt");
source = fopen(fname,"rb");
char outname[128];
printf("Enter .txt file name to print ");
scanf("%s",outname);
strcat(outname,".txt");
destination = fopen(outname,"wb");
unsigned char buffer[BUFFER_SIZE];
unsigned data;
int i = 0;
if (source) {
while (!feof(source)) {
n = fread(buffer, 1, BUFFER_SIZE, source);
count += n;
data =buffer[0];
// printf("MSG : ");
for (int i = 0; i < 3; i++)
{
//
// buffer[i] = map(buffer[i]);
printf(" %d", buffer[i]);
}
// printf("The contents of buffer[] are %s ", buffer);
fwrite(buffer, 1, n, destination);
}
printf(" %d bytes read from library. ", count);
} else {
printf("fail ");
}
fclose(source);
fclose(destination);
return EXIT_SUCCESS;
}
in the file i have 062434752
are teacher gave us
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.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);
for (int i = 0; i < 4; i++)
{
unsigned value = (data >> (18 - 6 * i)) & 077;
chars[i] = map(value);
}
printf("MSG : ");
for (int i = 0; i < 4; i++)
{
printf("%c", chars[i]);
}
printf(" ");
data = 0;
for (int i = 0; i < 4; i++)
{
unsigned value = unmap(chars[i]);
data <<= 6;
data += value;
}
printf("DATA: %o ", data);
return EXIT_SUCCESS;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.