Reading Hex Values to Strings from File

This is a discussion on Reading Hex Values to Strings from File within the C Programming forums, part of the General Programming Boards category; Hey, I'm trying to write a program that teaches you basic Danish vocabulary. Anyway, I originally had an array of ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9

    Thumbs down Reading Hex Values to Strings from File

    Hey, I'm trying to write a program that teaches you basic Danish vocabulary. Anyway, I originally had an array of structs with const chars for the dan word and eng word like so...

    Code:
    struct ord{
    char* dan;
    char* eng;
    }ords[ORDNUMMER];//ORDNUMMER defined earlier
    //some other stuff...
    ords[4].dan="some danish word";
    ords[4].eng="some english word";
    //and so on
    So, then I came up with this crazy idea... I want to read the values I'm assigning to each const string from a file of hex nummers. Here's the file, called testfire.dat:

    61 6c 6d 69 6e 64 65 6c 69 67 65 64 00000000 //Almindeliged
    6f 72 64 69 6e 61 72 79 2c 20 6e 6f 72 6d 61 6c 00000000 //Ordinary, normal
    61 72 62 65 6a 64 65 00000000 //arbejde
    77 6f 72 6b 00000000 //work
    62 65 73 c3 b8 67 65 00000000 //besøge

    I put C style comments in there so you can see what the values mean. They're not actually in the file. (Also, the 00000000 signifies the end of a word, as you'll see later)

    Please note I'm assuming UTF-8 encoding. The problem I'm having is with the last line, the ø character, hex c3 b8. I get a segmentation fault when I run it in the command line. It's interesting, because when I run it in gdb, I don't get a seg fault, but it skips over the last e in besøge:

    Running a.out
    Code:
    webmaster@sWAN:~/Hent/Tests$ ./a.out
    almindeliged
    ordinary, normal
    arbejde
    work
    Segmentation fault
    webmaster@sWAN:~/Hent/Tests$
    Running GDB
    Code:
    (gdb) run
    Starting program: /home/webmaster/Downloads/Tests/a.out
    almindeliged
    ordinary, normal
    arbejde
    work
    besøg
    
    
    Program exited normally.
    (gdb)
    Oh! Almost forgot the source...

    Code:
    for(k=0;k!=6;k++){
            do{
                    fscanf(mFile, "%x", &ords[i].dan[i]);
                    j=ords[i].dan[i];
                    printf("%c",ords[i].dan[i]);
                    i++;
            }while(j!=0x00000000);
            printf("\n");
    }
    It may or may not have been obvious, but I honestly don't know a thing about computer memory. So... what's going on?
    Last edited by 809AreaCode; 07-20-2011 at 10:53 PM. Reason: Taking out unnecessary information

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 809AreaCode View Post
    I want to read the values I'm assigning to each const string from a file of hex nummers. Here's the file, called testfire.dat:

    61 6c 6d 69 6e 64 65 6c 69 67 65 64 00000000 //Almindeliged
    So do you have a text file with the two text bytes of 6 and 1 to represent the first character, or is this just how it appears in your mind, and if you open it up in a text editor it's actually an 'A'?
    Quote Originally Posted by 809AreaCode View Post
    Code:
    fscanf(mFile, "%x", &ords[i].dan[i]);
    ...It may or may not have been obvious, but I honestly don't know a thing about computer memory. So... what's going on?
    Did you actually allocate space for ords[ i ].dan? Or are you using your pointer uninitialized here?


    Quzah.
    Last edited by quzah; 07-20-2011 at 11:01 PM. Reason: fixing tags
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Code:
    ords[i].dan[i]
    Does not match the structure definition you gave us:
    Code:
    struct ord{
    char* dan;
    char* eng;
    }ords[ORDNUMMER];
    That is unless you allocated memory for those pointers somewhere that you didn't show us. Which would help figuring out the problem. Additionally, avoid magic numbers as much as possible.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Location
    Godthåb, Grønland
    Posts
    9
    Actually, I forgot to mention... struct ord was rewritten:

    Code:
    struct ord{
    char dan[512];
    }ords[ORDNUMMER]; //no eng words here for simplicity
    Last edited by 809AreaCode; 07-20-2011 at 11:14 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This brings me back to my first question. Why are you storing them as hex instead of text? You're basically tripling the size of your file. I mean if you just want it an exercise for the sake of learning, ok, but if it's meant to be practical, it isn't. Anyway:
    Code:
    for(k=0;k!=6;k++){
            do{
                    fscanf(mFile, "%x", &ords[i].dan[i]);
                    j=ords[i].dan[i];
                    printf("%c",ords[i].dan[i]);
                    i++;
            }while(j!=0x00000000);
            printf("\n");
    }
    I don't think you want both of those indexes to be i. Shouldn't one be k? Like say:ords[ k ].dan[ i ]? You can also just compare directly:
    Code:
    do 
    {
        ...
    } while( ords[ k ].dan[ i ] != 0x00 );
    As is, your assignment to j isn't going to check for four consecutive 0x00 bytes anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    And I'm sure in your fscanf you meant to type ords[k].dan[i] vice ords[i].dan[i]?

    EDIT: too slow
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading two values from a file as one value?
    By Flotonic in forum C Programming
    Replies: 4
    Last Post: 04-08-2011, 05:53 PM
  2. Replies: 12
    Last Post: 09-23-2010, 11:49 AM
  3. reading in a text file containing hex values
    By gaza2k1 in forum C Programming
    Replies: 34
    Last Post: 02-29-2008, 06:15 PM
  4. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  5. Reading float values from a file??
    By neil_w_84 in forum C++ Programming
    Replies: 1
    Last Post: 02-23-2005, 07:48 AM

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