Click here to Skip to main content
15,881,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi friends
i have a file in c langage , this file contatins binary byte it means only 0 and one !
i want to collect every 8 bytes to ASCII and save in another file !
thank you

What I have tried:

C++
bit_files *tmp;
tmp = (bit_files *)malloc(sizeof(bit_files));
char a;
FILE *first_file,*final_file;
first_file=fopen("file1","r");
final_file=fopen("file2","w");
		
            tmp->bitBuffer <<= 1;
            tmp->bitCount = 0;
			
while (a=fgetc(first_file)!=EOF)
{
	if (a==0)
	{
	tmp->bitBuffer=tmp->bitBuffer<<=1;
	}
	else
	{
		tmp->bitBuffer=tmp->bitBuffer|=1;
	}
	tmp->bitCount++;
	if (tmp->bitCount==8)
	{
		fputc(tmp->bitBuffer,final_file);
		tmp->bitBuffer=0;
		tmp->bitCount=0;
	}
}
fclose(first_file);
fclose(final_file);
Posted
Updated 15-Aug-16 23:44pm
v3
Comments
Patrice T 15-Aug-16 6:58am    
Show an example of your "binary file"
Use Improve question to update your question.

A "binary file" doesn't contain just zero and 1, it contains binary data - which are unsigned char values each containing between 0 and 255 inclusive. If your data truly is the binary values 0 and 1 only per byte, then all you need to do is loop through collecting each eight bits in an unsigned char by using the << operator to move each bit up into place and then write each assembled byte to your output when it's finished.

But I'd start by looking at your binary file with a hex editor to make sure you aren't just misthinking the whole app!
 
Share this answer
 
Comments
yagami_md 15-Aug-16 5:48am    
you can modify my code to be like what you say?thanx
OriginalGriff 15-Aug-16 5:56am    
Yes, I could - but this is your work, not mine.
And it's not exactly complex. I'd really start by checking your input files...
A simpler way would be to read eight bytes at a time and assemble the ascii from the low order bits, something like:
C++
char			inputBuffer[8];
char			temp;
FILE*			binin;
int				i;

binin = fopen("ctest.txt", "rb");
if (binin == NULL)
    exit(1);
do
{
    int nIn = fread(inputBuffer, sizeof(char), sizeof inputBuffer, binin);
    if (nIn < sizeof inputBuffer)
    break;
    temp = 0;
    for (i = 0; i < 8; ++i)
    {
        temp |= (inputBuffer[i] & 1) << (7 - i);
    }
    printf("%c", temp);
} while(1);
printf("\n");
 
Share this answer
 
v2
Comments
yagami_md 16-Aug-16 5:01am    
my program on visual studio 2010 , c langage
Richard MacCutchan 16-Aug-16 5:31am    
I have modified the code to use pure C; please try it now.
yagami_md 16-Aug-16 5:37am    
thank you very much , all its well
thank you brother , you are good programmer thank you for your help
Richard MacCutchan 16-Aug-16 5:56am    
Happy to help. Not good, but I have been doing it for a long time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900