readin off a text file with commas
hello,
am writing a program that read a text file wit 1s and 0s as binary like and converts it into hex. my question is how can i read a text file with 1's and 0's separeted by a comman like 101010 , 1011101 , 010111 . 110011 , ...
here's wot ive written so far
Code:
#include <stdio.h>
#include <string.h>
int bin2dec(char *bin);
int main()
{
char bin[80] = "";
char *p;
int dec;
while(strcmp(bin,"0"))
{
FILE *fp = fopen("binary.txt","r");
FILE *ofp= fopen("hex.txt","r");
char lname[25];
char bin[80] ;
fgets(bin,80,fp);
fgets(bin, sizeof(bin), fp);
fclose(fp);
// check for and remove trailing \n
if ((p = strchr(bin,'\n')) != NULL)
{
*p = '\0';
}
dec = bin2dec(bin);
if (dec) fprintf(ofp,"Hexadecimal =
0x%02X" ,bin);
fclose(fp);
fclose(ofp);
}
getchar();
}
int bin2dec(char *bin)
{
int b, k, m, n;
int len, sum = 0;
len = strlen(bin) - 1;
for(k = 0; k <= len; k++)
{
n = (bin[k] - '0'); // char to numeric
value
if ((n > 1) || (n < 0))
{
puts("\n\n only 1 and 0!\n");
return (0);
}
for(b = 1, m = len; m > k; m--)
{
b *= 2;
}
// sum it up
sum = sum + n * b;
}
return(sum);
}