0

I want compare if both sequence are equals and i'm using the following code but comparation always return false.

=========================================================================

 // testecompare.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <Windows.h>

using namespace std;

string getCurrentDirectoryOnWindows()
    {
        const unsigned long maxDir = 260;
        char currentDir[maxDir];
        GetCurrentDirectory(maxDir, currentDir);
        strcat(currentDir, "\\l0gs.txt");
        return string(currentDir);
    }

    string ReadFileContent() {

        string STRING;
        string aux;
        ifstream infile;
        infile.open(getCurrentDirectoryOnWindows());
        while (!infile.eof())
        {
            getline(infile, STRING);
            return STRING;
        }
        infile.close();

        return "";

    }


int _tmain(int argc, _TCHAR* argv[])
{
     char str[MAXCHAR] = "";
    sprintf(str, "0x0%X", "1D203E5");

    cout << str << endl;
    cout << "File content: " << ReadFileContent() << endl;

    // if i have the string "0x01D203E5" in my txt file 

    if (_stricmp(str,ReadFileContent().c_str()) == 0) { 

    cout << "Contents are equals!\n"; 

}

    system("pause");
    return 0;
}

How make this comparation correctly?

Thank you very much.

  • 1
    Off topic: `while (!infile.eof())` is a bug. Read more here: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 May 29 '17 at 16:47
  • Unable to duplicate with my hack attempt at a [mcve]. What does your MCVE look like? – user4581301 May 29 '17 at 16:56
  • @user4581301, the question was edited. –  May 29 '17 at 16:59
  • 2
    BTW, do not give your variable names the same name as the type, such as `string STRING`. – Thomas Matthews May 29 '17 at 17:07
  • 1
    That `sprintf` call is wrong; `%X` expects a unsigned integer type, your passing a `const char*` conversion. And `return STRING;` as a non-conditional statement *inside* your loop seems pointless; there may as well not be a loop there. – WhozCraig May 29 '17 at 17:17
  • 1
    Use `sprintf(str, "0x0%s", "1D203E5");` instead. Or change `str` to a `std::string`: `string str = string("0x0") + "1D203E5"; ... if (str == ReadFileContent())` – Remy Lebeau May 29 '17 at 17:23
  • @RemyLebeau, now worked. Thank you. –  May 29 '17 at 17:29

1 Answers1

0

An easy trick for comparing instances of different types is to convert them to a common type then compare.

So for example:

std::string content(ReadFileContent());
std::string from_array(str)
if (from_array == content)
{
}

Edit 1: Working example
The code works.
Here is a working program:

#include <iostream>
#include <string>

int main()
{
    static const char text[] = "Hello";
    std::string         text_as_string(text);
    std::string         expected_str("Hello");
    if (text_as_string == expected_str)
    {
        std::cout << "Strings are equal: " << text_as_string << "\n";
    }
    else
    {
        std::cout << "Strings are not equal.\n";
    }
    return 0;
}

$ g++ -o main.exe main.cpp
$ ./main.exe
Strings are equal: Hello

Remember, the above code samples are compare entire or whole strings, not substrings. If you want to search for a key string within a larger string, that requires different functions.

Thomas Matthews
  • 52,985
  • 12
  • 85
  • 144