CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Nov 2013
    Posts
    3

    string comparing problem

    In my 96 line main.cpp code below, at lines 40-44 I can't get this loop to work:
    if(string_a==string_b)
    {
    printf("string_a and string_b match\n");
    return 0;
    }
    string_a and string_b are created from two identical files, named stringa.txt
    and stringb.txt, both being 3 byte files containing: abc

    If some kind person out there can take a few moments to create two such files
    (create these two files in your own way without using my writetext function).

    My frustration is that although these two 3 byte files are identical and although
    I've verified they each both contain 'abc' by printing string_a and string_b ...
    the fact remains that my loop at lines 40-44 fails to register that they match!

    Perhaps my problem arises when I rename the string from loaded_values to string_a
    and string_b, as per this code segment here:

    char * string_a = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), string_a);

    .. or maybe the tiny loadfile function is doing something it shouldn't, not sure.

    So grateful if someone can tell me what's wrong in the above code!? Thnx, Peter
    ............................................Code follows.......................................
    #include "mytools.h"
    #include <fstream>
    #include <string>
    #include <iostream>
    #include <stdio.h>
    #include "stdafx.h"

    void writetext(const char fname[], std::string str, const char method[]);

    std::string loaded_values;
    std::string string1;
    std::string string2;
    std::string string3;
    std::string string_a;
    std::string string_b;

    int main()
    {
    string1="abc";
    string2="def";
    string3="abc";
    printf("string1: %s\n",string1.c_str());
    printf("string2: %s\n",string2.c_str());
    printf("string3: %s\n",string3.c_str());

    writetext("stringa.txt", "abc", "w");
    loadfile("stringa.txt");
    char * string_a = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), string_a);
    //string_a[loaded_values.size()] = '\0'; // terminating 0 added
    printf("---string_a is %s\n", string_a); //printed to show it

    writetext("stringb.txt", "abc", "w");
    loadfile("stringb.txt");
    char * string_b = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), string_b);
    //string_b[loaded_values.size()] = '\0'; // terminating 0 added
    printf("---string_b is %s\n", string_b); //printed to show it

    if(string_a==string_b)
    {
    printf("string_a and string_b match\n");
    return 0;
    }

    //if(string1==string3)
    //{
    //printf("string1 and string3 match\n");
    //return 0;
    //}

    }

    void writetext(const char fname[], std::string str, const char method[])
    {
    FILE *fileText;
    const char *text = str.c_str();
    if(strcmp(method,"a")==0)
    {
    fileText = fopen (fname,"a");
    fprintf(fileText,text);
    fclose(fileText);
    }
    else if(strcmp(method,"w")==0)
    {
    fileText = fopen (fname,"w");
    fprintf(fileText,text);
    fclose(fileText);
    }
    else
    {
    printf("You entered wrong Parameter to access textfile\n");
    }
    }

    void loadfile(const std::string &fileName)
    {
    loaded_values="";
    std::ifstream file(fileName.c_str());
    std::string line;
    while(std::getline(file,line))
    //loaded_values+=line+"\n";
    loaded_values+=line;
    printf("loaded_values is: %s\n",loaded_values.c_str());
    }

    //Below's what's in mytools.h:
    //#ifndef GUARD_H
    //#define GUARD_H
    //#include <string>
    //#include <stdio.h>
    //extern std::string thestring;
    //void loadfile(const std::string &fileName);
    //void writetext(const char fname[], std::string str, const char method[]);
    //extern std::string loaded_values;
    //#endif

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: string comparing problem

    Summary of what you have ...

    Code:
    char * string_a = new char[loaded_values.size() + 1];
    char * string_b = new char[loaded_values.size() + 1];
    
    if(string_a==string_b)
    You should not compare c-style strings using ==
    You should use strcmp ...

    Code:
    if (strcmp(string_a,string_b) == 0)
    Also:

    You have global std::string variables named string_a and string_b , and you
    also have local char* variables with the same name. This can be confusing.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string comparing problem

    Quote Originally Posted by RichardsPeter39yahoo View Post
    Perhaps my problem arises when I rename the string from loaded_values to string_a
    and string_b, as per this code segment here:

    char * string_a = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), string_a);
    In addition to what Philp stated, why are you using char pointers when you are using std::string everywhere else? Your program has a memory leak due to you using new[] without a proper call to delete[].

    There is absolutely no reason I see why you need to resort to using char pointers. Stick with std::string.

    Also, please use code tags when posting code. The code you have is unformatted and very hard to read.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Nov 2013
    Posts
    3

    Re: string comparing problem

    Quote Originally Posted by Philip Nicoletti View Post
    Summary of what you have ...

    Code:
    char * string_a = new char[loaded_values.size() + 1];
    char * string_b = new char[loaded_values.size() + 1];
    
    if(string_a==string_b)
    You should not compare c-style strings using ==
    You should use strcmp ...

    Code:
    
    
    Also:

    You have global std::string variables named string_a and string_b , and you
    also have local char* variables with the same name. This can be confusing.
    ****************************************************
    Your suggestion that I instead use this:
    if (strcmp(string_a,string_b) == 0)
    worked fine. Plus I removed the 2 redundant globals. Thanks Philip!

  5. #5
    Join Date
    Nov 2013
    Posts
    3

    Re: string comparing problem

    Quote Originally Posted by Paul McKenzie View Post
    In addition to what Philp stated, why are you using char pointers when you are using std::string everywhere else? Your program has a memory leak due to you using new[] without a proper call to delete[].

    There is absolutely no reason I see why you need to resort to using char pointers. Stick with std::string.

    Also, please use code tags when posting code. The code you have is unformatted and very hard to read.

    Regards,

    Paul McKenzie

    Paul McKenzie[/QUOTE]
    ****************************************************
    I have lots of code samples from an old PC with Borland 5.5 C++
    hence the use of char (from an old code segment). Thanks for
    reminding me to flush the memory, if I understand you, I should do this:
    ................................
    writetext("starting.num", "2", "w");//in real life app this file should be set outside
    loadfile("starting.num");
    char * _startingnum = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), _startingnum);
    delete[] loaded_values;

    Right? Btw, I'm new to forums and don't know what a tag is,
    unless you were saying I should attach the actual code file.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: string comparing problem

    Quote Originally Posted by RichardsPeter39yahoo View Post
    ... I'm new to forums and don't know what a tag is,
    unless you were saying I should attach the actual code file.
    Check it out: Announcements (section "Information on posting");
    BB code
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: string comparing problem

    Quote Originally Posted by RichardsPeter39yahoo View Post
    ****************************************************
    I have lots of code samples from an old PC with Borland 5.5 C++
    hence the use of char (from an old code segment). Thanks for
    reminding me to flush the memory, if I understand you, I should do this:
    ................................
    writetext("starting.num", "2", "w");//in real life app this file should be set outside
    loadfile("starting.num");
    char * _startingnum = new char[loaded_values.size() + 1];
    std::copy(loaded_values.begin(), loaded_values.end(), _startingnum);
    delete[] loaded_values;

    Right?
    No. The loaded_values is a std::string. It is "_startingnum" that needs to be deallocated.

    But why do any of this when you could simply have done this:
    Code:
      std::string _startingnum = loaded_values;
    Regards,

    Paul McKenzie

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