My code convert text to Base64. I used the algorithm without bit operations. What do you think about my code?
#include <stdio.h>
#include <stdlib.h>
#define CRT_SECURE_NO_WARNINGS
#define BIN 2
#define MEMORYSIZE 10
void FromSixBitNumbToDec(char *number1, FILE *result){
char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int i = 0;
char out = 0;
int temp_numb = 0;
int resul = 0;
int number = atoi(number1);
int deg = 1;
while (i < 6) {
temp_numb = number % 10;
number /= 10;
resul += (temp_numb * deg);
deg *= BIN;
++i;
}
out = table[resul];
fprintf(result, "%c", out);
}
void FromDecToBin(int number, FILE *output){
int *numb = (int*)calloc(MEMORYSIZE, sizeof(int)); // Allocates memory for numbers with a minimum size
int memory = MEMORYSIZE;
int i = 0;
int k = 0;
while (number >= 2) { // Divide the number 10 number system on a finite number of the number system and the remnants of the division in the array. If memory is low, it increased by 2 times
numb[i] = number % BIN;
number = number / BIN;
++i; // Array size (numb2 array)
if (i == memory){
numb = (int*)realloc(numb, (BIN * memory) * sizeof(int));
memory *= BIN;
}
}
numb[i] = number; // The last remnant
k = i;
while (k != 7){ // Supplement to the number of 8-bit
k++;
numb[k] = 0;
}
int *numb2 = (int*)calloc(k + 1, sizeof(int)); // Allocates new array for number
memcpy(numb2, numb, (k + 1) * sizeof(int)); // Copy all digits with numb array
free(numb);
for (i = 0; i <= (k / 2); i++) { // Overturn number
memory = numb2[i];
numb2[i] = numb2[k - i];
numb2[k - i] = memory;
}
for (i = 0; i <= k; i++) {
fprintf(output, "%d", numb2[i]);
}
}
void toBase64(char *argv[]){
FILE *text = fopen("C:\\b.txt", "r");
if (NULL == text) {
printf("Error!");
fclose(text);
return;
} else {
printf("File was opened\n");
FILE *output = fopen("C:\\output.txt", "w");
/*fpos_t position;
fgetpos(output, &position);*/
char c = 0;
while (fscanf(text, "%c", &c)!= EOF) { // Convert symbol to Binary
FromDecToBin(c,output);
}
fclose(text);
fclose(output);
//fsetpos(output, &position); // Don;t work because I used fclose(output) and open output2. Please Understand how I gotta keep it real!
//printf("pos = %d\n", position);
FILE *output2 = fopen("C:\\output.txt", "r");
FILE *result = fopen("C:\\result.txt", "w");
char *number = (char*)calloc(6, sizeof(char));
int i = 0;
char temp = 0;
while (!feof(output2)) {
while (i < 6) { // Take successively six bit and convert to Base64
if (!feof(output2)){
fscanf(output2, "%c", &temp);
number[i] = temp;
printf("%c", number[i]);
i++;
} else {
i /= BIN;
for (; i > 0; i--){
fprintf(result, "%c", '=');
}
return;
}
}
printf("\n");
FromSixBitNumbToDec(number, result);
i = 0;
}
fclose(output2);
fclose(result);
}
}
int main(int argc, char *argv[]){
if (1 < argc){
printf("Argument has been Received\n");
toBase64(argv);
} else {
printf("Need some arguments!\nExit... \n");
return 1;
}
return 0;
}