readin off a text file with commas

This is a discussion on readin off a text file with commas within the C Programming forums, part of the General Programming Boards category; hello, am writing a program that read a text file wit 1s and 0s as binary like and converts it ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    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);
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    How about adding another if() or two to check for spaces and commas?

    Is your code as badly indented in your editor as it is on the forum?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    ey

    be nice will ya... am only askin ya a question like theres no need for all that


    tara mate

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    All what?

    Being able to format code is one of the first lessons, so you may as well get used to the idea.

    Not only will you find that code will be much easier to write and follow yourself, it will also encourage people who might otherwise gloss over your posts into actually helping you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03:49 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21