Here my question I need a decoder for binary codes from a text file but the problem is when I put a space in the text file tpos values crash I know what the problem is the while(fgets(buf, 8, fb) != NULL) because some binary codes are only 6 characters long like the space 100000 binary codes and the numbers unlike the letters which are 7 binary codes long can you guys help me come up with a expression or what should I do in these situation tnx in advance

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

int decimalConverter(int num)
{
int bnum, dec = 0, base = 1, rem ;

bnum = num;
while( num > 0){
rem = num % 10;
dec = dec + rem * base;
num = num / 10 ;
base = base * 2;
}
return dec;
}
main()
{
char buf[50];
int a, ab;
char abc;
FILE *f;
FILE *fb;

f = fopen("test.txt","r");
fb = fopen("encoder.txt","r");


while(fgets(buf, 8, fb) != NULL){
a = atoi(buf);
ab = decimalConverter(a);
abc = (char)ab;
printf("%c", abc);
}
printf("\n");



}

the text file looks like this or somthing like this

Text File
100000101100011000001001010

Edited 5 Years Ago by anjoz: n/a

There's nothing you can do in this situation. The file needs some kind of formatting to differentiate between codes, such as a separator or a guaranteed field width. If you can have codes of varying length all mushed up next to each other and no way to determine where the boundaries are, you're SOL.

should i make a formatting function or should i start all over again?

The problem is not your code, it's the file. If you can't unambiguously figure out what the codes are without intuition, you can't write code to do it. The file needs some kind of formatting that facilitates separation of codes.

heres an update on what i have done the problem is example ia a text file i have 1234 popo when i encode it the right one is 011000101100100110011011010001000001110000110111111100001101111 but what happens is it prints 011000101100100110011011010001000001110000110111111100001101111000101000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

i know what to problem is it reads the full lenght of the buf array can you gets help me if want im in the right track or give me tips on how i can solve this problem

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

int ascConverter(char ch)
{
	int ascVal;
	ascVal = toascii(ch);
	return ascVal;
}
void binaryConverter(int num,FILE **myFp)
{
	int rem, i = 1, counter = 7;
   	int bin = 0;
	char buffer[20];

	
	   while(num > 0){
      		rem = num % 2;
      		num = num / 2;
      		bin = bin + (i*rem);
      		i = i * 10;
		counter--;
   	   }
	sprintf(buffer, "%d", bin);
	while(counter != 0){
		fprintf(*myFp,"0");
		counter--;
	}
	fprintf(*myFp,"%s",buffer);	
}
int decimalConverter(int num)
{
	int bnum, dec = 0, base = 1, rem ;

	bnum = num;

	while( num > 0){
		rem = num % 10;
		dec = dec + rem * base;
		num = num / 10 ;
		base = base * 2;
	}
	return dec;
}

int main(int argc, char *argv[])
{
	int i;
   	char convert;
   	FILE *type;
   	FILE *fb;
   	FILE *decoder; 
	FILE *c;
   	char buf[50] = { };
   	type = fopen(argv[2],"r");
   	fb = fopen(argv[3],"w+");
	decoder = fopen(argv[3],"r+"); 
	
	if(strcmp(argv[1],"encoder") == 0){	
   		while(fgets(buf, sizeof(buf), type) != NULL){
			
			for(i = 0;  i < sizeof(buf);i++){
				convert = buf[i];
				int conv = ascConverter(convert);
				binaryConverter(conv, &fb);
			}
			memset (buf, '\0', sizeof(buf));
		}
	}
	else if(strcmp(argv[1],"decoder") == 0){
		char decodeBuf[50];
   		int toInt, toDecimal;
   		char toChar; 
			while(fgets(decodeBuf, 8, type) != NULL){
				toInt = atoi(decodeBuf);
				toDecimal = decimalConverter(toInt);
				toChar = (char)toDecimal;
				fprintf(decoder,"%c",toChar);
    			}
	 		fclose(decoder);
	}   
	else{
		printf("Invalid Argument");
   	}
	fclose(fb);
	fclose(type);
	return 0;
}

done close this thread already

Comments
Don't ask us to close a thread -- YOU mark the thread solved.
This question has already been answered. Start a new discussion instead.