Advertisement

Coding Question

Started by April 29, 2001 05:48 AM
1 comment, last by mk83 23 years, 9 months ago
Hi Guys, This is proberly a Newbie question, so please don''t flame, I am trying to use WIN API for the first time, and also at the same time trying to learn C++, I have a small problem! I add an array of char to a List box with the following command while (fBox.read((char*) &BoxData, sizeof BoxData)) { SendMessage(hLBBoxes,LB_ADDSTRING,NULL,(LPARAM) BoxData.sBox); }; I later in another part of my Program check which iof the values the user has selected with the following int SelectedBox; char Box[4]; SelectedBox = SendMessage(hLBBoxes,LB_GETCARETINDEX,0,0); SendMessage(hLBBoxes,LB_GETTEXT,SelectedBox,(LPARAM) (char*) Box); Now when I debug, the value of Box is "DEV\0" the \0 is supposed to be the terminating caracter ? correct ? I then Loop to find that Box while (fBox.read((char*) &BoxData, sizeof BoxData)) { if (BoxData.sBox == Box) { //Do stuff here }; }; The problem comes when I look at the value of BoxData.sBox, it is just "DEV", without the terminating character. When I add to the fBox File it does have a terminating character!! Now the compare does not become true, when the correct Box is found!!! Any hints would be appreciated MK83
while (fBox.read((char*) &BoxData, sizeof BoxData))
{
if (BoxData.sBox == Box)
{
//Do stuff here
};
};

keep in mind that C-strings are just arrays, you are not actually comparing the contents of the arrays, but their starting addresses

to compare two null-terminated strings, include string.h and use strcmp(), it returns a positive value when s1 > s2, 0 when s1 == s2, and a negative value when s1 < s2, like this:

while (fBox.read((char*) &BoxData, sizeof BoxData))
{
if (strcmp(BoxData.sBox,Box) == 0)
{
//Do stuff here
};
};


Advertisement
Great, It works perfectly.

Thanks for the help,
Cheers,
MK83

This topic is closed to new replies.

Advertisement