sscanf troubles

This is a discussion on sscanf troubles within the C Programming forums, part of the General Programming Boards category; I have an App that writes "Unicode-encoded" hex escaped chars to a file using: sprintf(buf, "0x%x", test_string[i] & 0xFFFF); write ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    sscanf troubles

    I have an App that writes "Unicode-encoded" hex escaped chars
    to a file using:

    sprintf(buf, "0x%x", test_string[i] & 0xFFFF);
    write buf to file...

    where test_string is a wchar_t

    I now need to read this back into another app for rendering.

    I am pretty sure I need to use sscanf, but I am not sure how to get it back into wchar_t format.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you are writing them one per line, then you can read each line into a buffer, then use:

    sscanf(buf, "0x%x", &test_string[i]);

    Or you can read it directly from the file with:

    fscanf(fp, "0x%x\n", &test_string[i]);

    If you aren't writing each on a separate line, then it's not as easy. But you can make it easy by making each part written 6 bytes:

    sprintf(buf, "0x%04x ", test_string[i] & 0xFFFF);

    That way you can read in six bytes, and use sscanf():

    sscanf(buf, "0x%x", &test_string[i]);

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    Thanks for the info.

    I'm wondering now if there would be a better way to store my strings.

    My input files will contain other information than text.
    An example record would be:
    <font,font size,r,g,b, xpos, ypos, string>

    Would it be possible to store a utf-string in the string portion?

    If so, would it be relatively painless to do any necessary conversions and load it into a wchar_t?

    I am aware of the iconv function, but I have yet to find any useful examples of how it would apply in my case.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >Would it be possible to store a utf-string in the string portion?

    You kind of lost me. Can you explain it a little better? And also, what is a utf-string?

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    4
    It is my limited understanding that unicode can be expressed in utf springs by combinations of characters.

    For example, ";uP" represents two CJK(chinese, japanese, korean) characters. ";u" being the first and "P" being the second.

    I was just wondering if something like that might be a simpler approach.

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    It sounds like it would be simpler. You could just read in:

    <font,font size,r,g,b, xpos, ypos, string>

    as a string with fgets(), then parse each part.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using sscanf fgets and overflow checking
    By jou00jou in forum C Programming
    Replies: 5
    Last Post: 02-18-2008, 06:42 AM
  2. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 01:46 AM
  3. sscanf question
    By Zarkhalar in forum C++ Programming
    Replies: 6
    Last Post: 08-03-2004, 08:52 PM
  4. sscanf (I think)
    By RyeDunn in forum C Programming
    Replies: 7
    Last Post: 07-31-2002, 09:46 AM
  5. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM

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