-
September 19th, 2005, 04:12 PM
#1
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)
-
September 19th, 2005, 05:01 PM
#2
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"
-
September 19th, 2005, 05:33 PM
#3
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++;
}
-
September 19th, 2005, 06:18 PM
#4
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
-
September 19th, 2005, 06:23 PM
#5
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.
-
September 19th, 2005, 06:31 PM
#6
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
-
September 19th, 2005, 06:36 PM
#7
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...
-
September 19th, 2005, 09:21 PM
#8
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
-
September 19th, 2005, 10:41 PM
#9
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 ?
-
September 20th, 2005, 07:29 AM
#10
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
-
September 20th, 2005, 11:35 AM
#11
Re: Read Unicode into Struct
You are welcome..
-
October 4th, 2005, 12:27 PM
#12
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
-
October 4th, 2005, 04:57 PM
#13
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
-
October 4th, 2005, 05:03 PM
#14
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
-
October 4th, 2005, 05:08 PM
#15
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|