fgetc() and TABS, Extended ASCII

This is a discussion on fgetc() and TABS, Extended ASCII within the C Programming forums, part of the General Programming Boards category; Hi all, I am reading characters from a file input stream and printing out their 1. ASCII value or control ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    2

    fgetc() and TABS, Extended ASCII

    Hi all,

    I am reading characters from a file input stream and printing out their 1. ASCII value or control mnemonic 2. Binary Value 3. Hexidecimal Value.

    My problem is if the the current character I am reading in and then outputing is a TAB it prints an actual tab to screen. I would like instead to be able to print "TAB" for example.

    FILE *fp;
    inchar = fgetc(fp)

    printf("%x",inchar);

    etc.

    Any help would be appreciated.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    if (inchar == '\t')
      printf("TAB");
    else
      printf("%x", inchar);

  3. #3
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,156

    Re: fgetc() and TABS, Extended ASCII

    Originally posted by eddiemjm
    Hi all,

    I am reading characters from a file input stream and printing out their 1. ASCII value or control mnemonic 2. Binary Value 3. Hexidecimal Value.

    My problem is if the the current character I am reading in and then outputing is a TAB it prints an actual tab to screen. I would like instead to be able to print "TAB" for example.

    FILE *fp;
    inchar = fgetc(fp)

    printf("%x",inchar);

    etc.

    Any help would be appreciated.
    By control mnemonic do you mean STX, TAB, ESC, etc? You need to create a string array for all the characters below 0x20
    Code:
    char *CtrlArray[] = {"NUL", ... ,"BKSP", "TAB", ... };
    and during your loop, do something similar to what Thantos suggested:
    Code:
    if (inchar <  ' ')
      printf("%s", CtrlArray[inchar]);
    else
      printf("%x", inchar);
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

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