Thread: Char troubles !!!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    8

    Unhappy Char troubles !!!

    Folks...a very frustrating problem........pls. help.

    I am reading a input file having special characters like
    ^L^F^H^I^K^M ...... and so on, among other data.

    I am reading the input file using fread.

    char *buf ;
    Allocate memeory for buf ;
    fread(buf,1,LEN,fp);
    /* I come to know about the value of LEN just before fread
    LEN gives me the number of special characters on current line
    in the input file */

    Point to be noted each '^L','^F'..... are being stored as 'single'
    characters in 'buf'.

    After these characters I go on and read other data from the input
    file and process it.

    In the program I have to convert the special characters to their
    decimal values.Their decimal values are given in the ASCII table.

    I first tired used 'atoi' which obviously didn't work !!!.

    Now I have declared a static multidimensional char. array.
    A sample :

    static char *table[2][33] = {
    {"^@", "^A","^B","^C","^D",..............} ,
    { "1" , "2" ,"3", "4", "5", ................. }
    };


    My problem is I cannot even use 'strcmp' to compare. 'strcmp'
    just fails and dumps core .
    Because spl. chars. read from input file as being read/stored as
    single characters and my array treats each spl. char as strings.

    How do i get the decimal equivalent of such characters in this
    case. I am working on SOLARIS platform.


    Any ideas ....???
    I am really stuck and have no idea how to do it .

    Appreciate all kinds of suggestions.


    Thanks.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is two-fold:

    1) You cannot use strcmp on binary values (very well, as there is the potential for a null to be in there).
    2) Your string does not treat "^h" as a single character when you have it in your example.

    This is like: '\n' is a single character. The same goes for control keys and "unprintable" characters.

    Quzah.

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    What kind of car troubles are you having?

  4. #4
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Wink

    You can convert the characters to their Hex value and then compare them with Hex values of an ASCII table.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    Troll_King.......I thought I will post a snippet of code that I tried
    on MS VC++6.0 on my PC at home. It works fine.


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

    #define MAX 2


    static char *table[MAX][33] ={

    { "^@","^A","^B","^C","^D","^E","^F","^G","^H","^I", "^J","^K","^L","^M","^N","^O","^P","^Q","^R","^S", "^T","^U","^V","^W","^X","^Y","^Z","^[","^\\","^]","^^","^_","SP"} ,
    { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10","11","12","13","14","15","16","17","18","19", "20","21","22","23","24","25","26","27","28"," 29", "30","31","32"}

    };

    /** My test input data was ^[^^^_^\SP^L^Z^@^A^F^K^M^N in a file **/

    void main()
    {
    char *str,*strstart,*temp;
    char ch ;
    int val=0 ;
    FILE *fp ;

    str = (char *) malloc(sizeof(char)*SIZE);
    strstart = str ;
    temp = (char *) malloc(sizeof(char)*2);


    if((fp = fopen("input.txt","rb")) == NULL)
    printf("Error opening file \n");


    while(!feof(fp))
    {
    fread(str,1,SIZE,fp);
    printf("Input String is .%s.\n",str);
    }

    while( *str != '\0')
    {
    strcpy(temp,"");
    strncat(temp,(const char *)str,2) ;
    printf("Temp string is .%s.\n",temp);
    /** Incr. str twice so as to get next vector starting with '^' **/
    str++ ;
    str++ ;
    if( !(isprint(temp[0])) )
    {
    printf("BREAKING\n");
    break;
    }

    val = 0 ;

    while( val < 33)
    {
    if( (strcmp(temp,table[0][val])) != 0)
    printf("In if\n");
    else
    {
    printf("In else\n");
    break ;
    }
    val++;
    }

    printf("VAL FOR .%s. is %d\n",table[0][val],atoi(table[1][val]));


    }

    The same thing does not work on SOLARIS as the
    input vectors like '^A' is read as one char. while as
    in the above code '^A' is read as two characters.

    Once again I have to get decimal equivalent of chars. like
    '^A','^^' which are read from input file.

    Is there a way out ??

    Thanks.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    Engineer,


    How do I convert the input chars to Hex. ??

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    there is no inbuilt datatype in C called hex , you might cast the char to an int and then view it as hex ,
    like,

    char c='a';
    int t;
    t= (int) c;
    printf("%x",t);

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include<stdlib.h>
    
    int main( ) 
    {
    	char c;
    	FILE *fptr = fopen("c:\\Accountsout.txt","r");
    	if(fptr == NULL) 
    	{
    		printf("Error");
    		exit(1);
    	}
    
    	while( fscanf(fptr,"%c",&c) != EOF)
    	{
    		printf("%x ",c );
    	}
    	fclose(fptr);
    	return 0;
    }
    You can convert char to hex with the %x specifier. You can change printf to fprintf or even sprintf depending on where you want the converted data to go.

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Could be that you need to change it to int first. That's true.

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Hmmm

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    But it just prints the value in Hex. It does not help me to
    convert, unless I missed out on sometihng.

  12. #12
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Where do you want it?

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    8
    Also characters I have are control characters not simple
    characters. Way out suggested by you guys will work if they are
    simple characters like 'a' , 'A' ...etc......
    How do i do for control characters like '^L','^B'.

  14. #14
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Okay give me some time, say an hour. But you just want to print out everything including special characters as their acii integer equivalent? Is that what we are trying to do here?

  15. #15
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125
    stmifx:

    It's really late and I am usually brain dead at this time of day, but tomorrow I will post the code you need to convert chars to Hex. I just have to remember how it is done. So if you can wait till tomorrow, I will gladly help you.

    Cheers.
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. Replies: 6
    Last Post: 06-30-2005, 08:03 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM