Problem comparing string from text file with string constant

This is a discussion on Problem comparing string from text file with string constant within the C++ Programming forums, part of the General Programming Boards category; The following code does not work... could someone explain? char StringFromFile[10]; char ConstantString[10] = "RootFolder"; //This is a Win32 Application. ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    31

    Problem comparing string from text file with string constant

    The following code does not work... could someone explain?

    char StringFromFile[10];
    char ConstantString[10] = "RootFolder";

    //This is a Win32 Application. I can't remember the parameters
    //for int Main.... but I'm sure you get the idea.

    int Main()
    {

    ifstream DataSource("Dat.txt");

    DataSource.getline(StringFromFile, 10);

    if (StringFromFile == ConstantString)
    {

    MessageBox(NULL, StringFromFile, "Just a test", MBOK);

    }
    else
    {

    MessageBox(NULL, "The text did not match", "Just a test", MBOK);

    }

    return 0;

    }

    The problem is that the comparison always returns false and displays the "The text did not match" messagebox...

    Is it because the line read has a newline character? I'm not sure how to remove it if this is the case. Any advice?
    Welcome to the funhouse, where strange mirrors reflect the faces of insanity.

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Here's your problem:

    if (StringFromFile == ConstantString)

    You are actually comparing the pointers, not what they point to. In other words, you are say is the memory address of StringFromFile equal to the memory address of ConstantString.

    What you need to do with c strings is:

    if (strcmp(StringFromFile, ConstantString) == 0)

    Hope this helps.

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    The manual page: strcmp

  4. #4
    Registered User
    Join Date
    Jun 2002
    Posts
    31
    Davros: Thanks for the explanation. I haven't had much exposure to pointers yet so I make mistakes like that often.

    Guess I know what I'll be reading tonight...


    Monster: Thanks for the link... that will prove to be a useful "favorite"
    Welcome to the funhouse, where strange mirrors reflect the faces of insanity.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  2. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 09:01 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 06:35 PM

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