CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Aug 2005
    Posts
    59

    Read Unicode into Struct

    I have a file with a series of words beginning at offset 1440 and running for a variable number of bytes until the end of file. It does consist of exactly 18 words. Each of the words is in Unicode.

    I have written an ifstream statement as follows where
    nBytes =1440
    l and m are long and I find the size of buffer required by going to end of file and subtracting 1440

    Code:
    ifstream filef((LPCTSTR) dlgFile.GetFileName(), ios::in|ios::binary|ios::ate);
    filef.seekg (0, ios::end);
    m = filef.tellg();
    int catBytes = m-l;
    filef.seekg (0, ios::beg);
    filef.seekg (nBytes, ios::beg);
    catbuffer = new char [catBytes];
    filef.read (catbuffer, catBytes);
    filef.close();
    I have a struct defined as follows
    Code:
     struct categories {
    	             char category;
    	             }catarray[18];
    Question is how do I get the catbuffer into the catarray. If it helps the format of the unicode is B.r.o.w.n...G.r.e.e.n...O.r.a.n.g.e...
    etc

    Thanks for any guidance....Andrew
    (C++ MFC App using FormView)

  2. #2
    Join Date
    Apr 2001
    Location
    Tampa Florida
    Posts
    233

    Re: Read Unicode into Struct

    Hey Mogul
    Thats a lot of code but i would let the computer do the work.
    I would read the entire file into a cstring then .replace the seperator ... ?
    with a delimiter like ascii 10 then extract the substrings into the array with
    ::afxExtractSubString( array[x], fullstring, x, 10 ) in a for loop of 18.
    Regards
    "trampling out the vintage"

  3. #3
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    First off, you haven't specified what l is , is it 1440 too ?

    Anyways, what you need to do is, have a WCHAR array in your struct instead of simply a char. I assume you are goint to have 18 wide char strings.
    Then you need to keep looping through the buffer one by one and copy them to the member data of the structure . When you hit the '\0' terminator, you know you need to advance to the next structure.

    Some sample code below:
    Code:
    typedef struct
    {
    	WCHAR szData[50];
    } category;
    category g_CatArray[18];
    Code:
    int nIndexOfStruct = 0;
    int nIndexOfData = 0;
    WCHAR* pData = (WCHAR*)catbuffer;
    
    for(int j = 0 ; j < sizeof(catbuffer)/sizeof(WCHAR); j++)
    {
    	g_CatArray[nIndexOfStruct].szData[nIndexOfData] = (WCHAR)(*pData);
    	if((*pData) == '\0')
    	{
    		nIndexOfStruct++;
    		nIndexOfData = 0;
    	}
    	else
    	{
    		nIndexOfData++;
    	}
    	pData++;
    }

  4. #4
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    Kirants,

    I am not sure I follow, I think I understand that what you are suggesting is that the category in the struct is itself made up of a char array of szData

    We then read the buffer and keep extracting letters until we hit a null and then we write the szData array to category and repeat for all 18

    However, since pdata will contain the whole string "Green Brown Orange" the comparison if((*pData) == '\0') will never be true until the very last byte and therefore will only write 1 entry rather than 18...

    So I am assuming I need compare something that is only looking at the next byte in pData and compare that to Null...

    Any ideas....Thanks

  5. #5
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    You are right. The struct cannot be just a char. You need 18 structs , each one having enough space the copy the string , right ?

    Hence the WCHAR szData[50]; 50 assuming is enough for the strings.
    keep extracting letters until we hit a null and then we write the szData array to category and repeat for all 18
    That is right, however , as we extract , we also copy to the current structure like below:
    Code:
    g_CatArray[nIndexOfStruct].szData[nIndexOfData] = (WCHAR)(*pData);
    So, that copies all data including the NULL terminator.
    After copying, if we have it a NULL terminator, we advance to the next structure.

  6. #6
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    yes but when I debug this I get

    g_CatArray[0]= "Brown Green Orange"

    Instead of
    g_CatArray[0] = "Brown"
    g_CatArray[0]= "Green"
    g_CatArray[0]= "Orange"

    Because the condition I listed is never met and so

    nIndexOfStruct++;
    nIndexOfData = 0;

    Does not execute until the last Null is read and the whole string is read into one element

    Am I being think here?

    hehe
    Andrew

  7. #7
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    I would have thought your buffer had NULL terminatorsin between like you showed. Isn't that so ?

    Code:
    B.r.o.w.n...G.r.e.e.n...O.r.a.n.g.e...

  8. #8
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    yes that is correct....After the first pass through the loop pdata is equal to rown green orange....After the second pass through it is equal to own green orange

    So when it gets to the first space pData = " green orange" which is not qual to NULL so it keep on going

    After one more pass through pdata = reen orange and the array = Brown g

  9. #9
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    I don't know.. I can't say unless you upload the file that contains the strings. It appears that the .. there is not NULL terminator, but something else.
    Open the file with a binary editor ( or open it in visual studio as binary instead of text ) and see what those actual bytes are. If it is in fact a terminator, it should be 00 00.

    Also, what does *pData say at that location if you watch the variable ?

  10. #10
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    Thanks...you nailed it....I looked in hex editor at the ascii to check for ... between each variable but on closer examination of the bytes they were not NULL eg 00 08 00 changing them to 00 fixed the problem....thanks for sticking with me...Regards, Andrew

  11. #11
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    You are welcome..

  12. #12
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    Ok, so I am trying to take the structured array and populate the variables into a comboBox. I am using the following code

    Code:
    int nIndex;
    int a;
    for (a=0; a < 18; a++)
    {
    nIndex = m_currtex.AddString((LPCTSTR)&g_CatArray[a]);
    m_currtex.SetItemData(nIndex,(DWORD)&g_CatArray[a]);
    }
    But dor some reason it is only picking up the first letter of each word in the array. I feel it has something to do with strings and nulls in C but not sure..

    thanks for any feedback...A

  13. #13
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    Ok I think I am homing in on the problem...

    The g_CatArray is a Wide Char array. As a result I think it may be terminating at the second byte. In order to feed the dialog I am assuming I have to convert the wCHAR to CHAR or CSTRING.

    This of course will have to be done letter by letter I assume using a for loop...

    A

  14. #14
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: Read Unicode into Struct

    Either that. Or change your project settings to use UNICODE. Is that an option ?

    Anyways, see this article:
    How to Display UNICODE Characters in a CListCtrl or a CEdit

  15. #15
    Join Date
    Aug 2005
    Posts
    59

    Re: Read Unicode into Struct

    Its my project I guess I can do anything....hehe

    EDIT:

    Oops...guess not...Adding Unicode to the project properties C++ section caused a whole bunch of errors...so guess thats out...

    A
    Last edited by Mogulbasher; October 4th, 2005 at 05:28 PM.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured