IF-ELSE problems. Why?

This is a discussion on IF-ELSE problems. Why? within the Windows Programming forums, part of the Platform Specific Boards category; I have this code: Code: hFile=CreateFile("Settings.ini", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); ReadFile(hFile, Buffer, 3, &dwBytes, NULL); if(Buffer=="foo"){ Beep(2000,2000); } ...

  1. #1
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

    Question IF-ELSE problems. Why?

    I have this code:
    Code:
    hFile=CreateFile("Settings.ini", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    ReadFile(hFile, Buffer, 3, &dwBytes, NULL);
    if(Buffer=="foo"){
    Beep(2000,2000); }
    else {
    Beep(500,500); }
    But even when my ini file contains the text "foo" the program still returns my if-else statment as false. Why is it doing this? The method works fine in DOS programming.

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Was it a dumb question?

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    You can compare strings like that.... try lstrcmp

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You should also be checking the return values from CreateFile and ReadFile to ensure they are working as expected.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I dont get it. What do you mean by "try lstrcmp"? In what way?

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    It's a winapi variant of strcmp - see lstrcmp.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    It doesn't seem to be working, could you give me an example?

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    Traditionally, one posts troublesome code and requests advice on that.

    Code:
    TCHAR buffer[]=_T("hello");
    if (lstrcmp(buffer,_T("hello"))==0)
      {
      /*match found*/
      }
    else
      {
      /*strings not the same*/
      }
    Note the lstrcmp is case sensitive; if you want case insensitivity then use lstrcmpi.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    I tried that but it doesn't matter what the text is it always returns as true.

  10. #10
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Oops... Sorry, I declared Buffer twice, over writeing what ReadFile picked up.

    Thanks for showing me how to do it Ken.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 05:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

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