I'm writing a program that reads a file and generates an array of integers of each byte, first I prove with a .txt file and I generates the ASCII for each of the letters and its corresping byte(for example the ASCII for B is 66, and the binary of 66 is 01000010, my program print this two in the .exe window), but I need to make the same file with the integers of the array(rest[x]), I could make a new file .txt but it has only trash, or I think is it, file.txt is the name of the new file .txt and it only has trash, I want to recover the text that is in UAM.txt, and writte in file.txt from the array rest[x];
#include<stdio.h>
int main()
{
int b1, b2, b3, b4, b5, b6, b7, b8;
int x, i;
char a, b;
FILE *f1, *bin, *fp;
b1 = 0x01; // = 0000 0001
b2 = 0x02; // = 0000 0010
b3 = 0x04; // = 0000 0100
b4 = 0x08; // = 0000 1000
b5 = 0x10; // = 0001 0000
b6 = 0x20; // = 0010 0000
b7 = 0x40; // = 0100 0000
b8 = 0x80; // = 1000 0000
int mask[8] = { b8, b7, b6, b5, b4, b3, b2, b1 };
int rest[8];
f1 = fopen("UAM.txt", "rb");
fp = fopen("file.txt", "w+");
while (!feof(f1))
{
a = getc(f1);
printf("%d\n", a);
for (i = 0; i <= 7; i++)
{
rest[i] = a & mask[i];
}
for (i = 0; i <= 7; i++)
{
rest[i] = rest[i] / mask[i];
}
for (x = 0; x <= 7; x++)
{
printf("%i", rest[x]);
fputc(rest[x], fp);
}
fclose(fp);
printf("\n");
}
return 0;
}
the input file can be any text, for a easy example I save the word B in UAM.txt, and in the .exe window I obtained 66 and 01000010 which is the ASCII code for B in decimal and binary, next the binary number which is the byte of the word is located In an integer array(rest[x]). I need to convert this binary again to the letter B, and save this letter or any text in a new file that I named file.txt, in this file has to be again the letter B, or any text that it´s in UAM.txt sorry for my poor english!
any help will be appreciated!