" IF "-Problem (GetPrivateProfileInt)

Hallasas

Erfahrenes Mitglied
Hallo, ich bins mal wieder...
Ich habe folgendes Problem:
Die "auth.ini" hat folgenden Inhalt:
Code:
[Authentication]
user=username
pass=password
Dies ist mein Programcode:
Code:
#include <fstream.h>
#include <windows.h>
#define DEFAULTVALUE_MATCHES (4711)
using namespace std;

int main()
{
    ifstream authfile("auth.ini");
    if(authfile)
    {
        return 0;
    }
    else
    {
        MessageBox(NULL, "Authentication failed!", "Fatal Error",
            MB_OK | MB_DEFBUTTON1);
        return -1;
    }

    char authuser = GetPrivateProfileInt("Authentication","user",
        DEFAULTVALUE_MATCHES, "./auth.ini");
    char authpass = GetPrivateProfileInt("Authentication","pass",
        DEFAULTVALUE_MATCHES, "./auth.ini");

    if(authuser == "username" && authpass == "password")
    {
        return 0;
    }
    else
    {
        MessageBox(NULL, "Wrong username and/or password", "Fatal Error",
            MB_OK | MB_DEFBUTTON1);
        return -1;
    }

    return 0;
}
Und hier gibt es das Problem:
Code:
...
if(authuser == "username" && authpass == "password")
...
gcc meldet:
Code:
error: ISO C++ forbids comparison between pointer and integer
error: ISO C++ forbids comparison between pointer and integer
(Zwei mal: für "authuser" und "authpass")
Und nun meine Frage:
Wie kann ich die Syntax so "umstellen", dass wenn "authuser" und "authpass" richgtig sind, das Program weiter geht und wenn "authuser" oder "authpass" falsch sind, das Program eine MessageBox ausgibt, und sich dann beendet?

Ich freue mich über eure Hilfe
Gruß Hallasas
 
Wenn du Strings verarbeiten willst, musst du GetPrivateProfileString() benutzen. Was mit deinem Vergleich nicht stimmt, wird durch den Text der Fehlermeldung schon bestens erklärt.
Hier ein Beispiel, wie man's machen könnte:
C++:
#include <string>

// ...

string authuser, authpass;
char buffer[40];

if( GetPrivateProfileString( "Authentication",
                             "user",
                             "",
                             buffer,
                             sizeof(buffer),
                             "./auth.ini" ) )
{
    authuser = buffer;
}

if( GetPrivateProfileString( "Authentication",
                             "pass",
                             "",
                             buffer,
                             sizeof(buffer),
                             "./auth.ini" ) )
{
    authpass = buffer;
}

if( authuser.compare("username") == 0  && authpass.compare("password") == 0 )
{
    return 0;
}
else
{
    // ...
}
Gruß
MCoder
 
Hallo !!

Gibt es auch für "C" eine Funktion, womit man dieses Problem lösen kann, da C den Befehl nicht kennt ..
 
Hi.
Hallo !!

Gibt es auch für "C" eine Funktion, womit man dieses Problem lösen kann, da C den Befehl nicht kennt ..
Die Funktion GetPrivateProfileString ist eine C Funktion von Windows. Wenn du auf die Seite gehst die lmarkus31 verlinkt hat, siehst du ganz unten die "Requirements":
Header

Declared in Winbase.h; include Windows.h.
Library Use Kernel32.lib.
DLL Requires Kernel32.dll
D.h. man muß die Headerdatei "windows.h" inkludieren und die Bibliothek "kernel32" zum Programm hinzulinken. Wie das geht kommt drauf an welche(n) IDE / Compiler du benutzt.

Gruß
 
Zurück