Problem mit der while Schleife



  • Und das funktioniert? Ok. Dann würde ich gleich die Funktion TestFile weiternehmen, nicht alles in die main schreiben. Also irgendwas an deinem Datei-Zeugs stimmt nicht... bloß was?

    cya



  • Ja gut aber hier sind es ja auch nur 2 string normal sind es ca.90 die überprüft werden sollen, dabei wird dann doch ein wenig der code zu unübersichtlich.

    wenn du mir sagen könntest, wie ich die Funktion benutzen kann kombiniert mit dem datei handling.



  • In buffer steht ja dann die URL, die übergibst du einfach der Funktion. Lass mal den Debugger drüberlaufen, und prüf nach, ob das richtige aus der Datei gelesen wird.

    cya



  • ES FUNKTIONIERT!!!

    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <wininet.h> //wininet.lib linken
    #include <iostream>
    #include <string>
    #include <fstream.h>
    
    using namespace std;
    
    const DWORD FILE_OK = 3158066;
    
    bool TestFile(string path);
    HINTERNET hnet,hhost;
    
    int main()
    {
        char host[50];
    
        cout << "Hostname: "; cin >> host;
    
        hnet = InternetOpen("Inet",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
        hhost = InternetConnect(hnet,host,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
    
        if((hnet || hhost) == NULL)
            cout << "Error! Keine Verbindung\n";
    
        ifstream file;
        file.open("string.txt", ios::in);
    
        string adress;
        adress.resize(256);
    
        while(!file.eof())
        {
            // Scan-String aus Datei lesen
            file.getline((char*)adress.c_str(),adress.size());
    
            if(TestFile(adress))
                cout << "File: " << (char*)adress.c_str() << "[TRUE]\n";
                else
                cout << "File: " << (char*)adress.c_str() << "[FALSE]\n";
        }
    
        file.close();
    
        InternetCloseHandle(hhost);
        InternetCloseHandle(hnet);
    
        return 0;
    }
    
    bool TestFile(string path)
    {
        DWORD status = 0;
        DWORD size = sizeof(DWORD);
        bool result = false;
    
        HINTERNET hdata = HttpOpenRequest(hhost,"HEAD",path.c_str(),NULL,NULL,NULL,0,0);
    
        if(hdata == NULL)
            return result;
    
        HttpSendRequest(hdata,NULL,0,0,0);
    
        if(HttpQueryInfo(hdata,HTTP_QUERY_STATUS_CODE,&status,&size,0))
            if(status == FILE_OK)
                result = true;
    
        InternetCloseHandle(hdata);
        return result;
    }
    


  • geil 🙂

    cya



  • Es gibt doch noch ein kleines Problem und zwar wollte ich das ganze jetzt mit einer Grafischen Oberfläche machen. Nur bekomme da immer ein paar Fehlermedlungen was die 'string' deklaration anspricht:

    Undefined symbol 'string' in function URLStart
    Statement missing ; in function URLStart
    Undefined symbol 'adress' in function URLStart
    Declaration is not allowed here in function URLStart

    Ich habe die Bibliothek #include <string.h> eingebunden.

    void URLStart(HWND hWnd, HWND hURL, HWND hStatus, HWND hView)
    {
        char host_name[50];
        HINTERNET hnet, hhost, hdata;
    
        string adress; // hier
        adress.resize(256); // und hier
    
        SendMessage(hURL, WM_GETTEXT, 50, (LPARAM)(LPCSTR)host_name); // Die URL aus der EditBox Lesen
    
        hnet  = InternetOpen("Inet",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
            hhost = InternetConnect(hnet,host_name,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
    
        if((hnet || hhost) == NULL)
            SendMessage(hStatus, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Error, kann nicht Verbinden!");
    
        FILE* list = fopen("string.txt","rt");
    
         while(!feof(list))
            {
                // Scan-String aus Datei lesen
                fgets((char*)adress.c_str(),adress.size(),list);
    
                if(TestFile(adress))
                            SendMessage(hView, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Vorhanden!");
                else
    
                SendMessage(hView, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Nicht vorhanden!");
            }
    
        fclose(list);
    
        InternetCloseHandle(hdata);
        InternetCloseHandle(hhost);
            InternetCloseHandle(hnet);
    }
    
    BOOL TestFile(string path)
    {
        DWORD status = 0;
        DWORD size = sizeof(DWORD);
        BOOL result = FALSE;
    
        HINTERNET hdata = HttpOpenRequest(hhost,"HEAD",path.c_str(),NULL,NULL,NULL,0,0);
    
        if(hdata == NULL)
            return result;
    
        HttpSendRequest(hdata,NULL,0,0,0);
    
        if(HttpQueryInfo(hdata,HTTP_QUERY_STATUS_CODE,&status,&size,0))
            if(status == FILE_OK)
                result = TRUE;
    
        InternetCloseHandle(hdata);
        return result;
    }
    


  • Nene, so geht das nicht... string.h ist böse ;)! Du musst nur <string> einbinden und dann "using namespace std;" schreiben! Für die "normalen" String/Zeichen-Funktionen nimmt man auch nicht mehr string.h, sondern <cstring>.

    cya 🙂



  • Weil dies nicht so ganz funktioniert poste ich mal den vollständigen Code und die Fehlermeldungen:

    C:\Borland\BCC55\Bin>bcc32 -tW win.c -l wininet.lib
    Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
    win.c:
    Error E2141 win.c 5: Declaration syntax error
    Error E2147 win.c 14: 'string' cannot start a parameter declaration
    Warning W8057 win.c 58: Parameter 'hPrevInstance' is never used in function WinMain
    Warning W8057 win.c 58: Parameter 'szCmdLine' is never used in function WinMain
    Error E2451 win.c 141: Undefined symbol 'string' in function URLStart
    Error E2379 win.c 141: Statement missing ; in function URLStart
    Error E2451 win.c 142: Undefined symbol 'adress' in function URLStart
    Error E2140 win.c 152: Declaration is not allowed here in function URLStart
    Warning W8057 win.c 172: Parameter 'hWnd' is never used in function URLStart
    Error E2147 win.c 175: 'string' cannot start a parameter declaration
    Error E2294 win.c 181: Structure required on left side of . or .* in function TestFile
    Warning W8057 win.c 194: Parameter 'path' is never used in function TestFile
    *** 8 errors in Compile ***

    #include <windows.h> /* Einbinden der Windows Header Bibliothek */
    #include <wininet.h>
    #include <string>
    #include <stdio>
    using namespace std;
    HINTERNET hnet,hhost;
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); /* Prototyp der*/
    
    const char szAppName[] = "Responses of the Internet - [ ROTI ]";
    
    void URLStart(HWND hWnd, HWND hURL, HWND hStatus, HWND hView);
    
    BOOL TestFile(string path);
    
    const DWORD FILE_OK = 3158066;
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
    {
        HWND       hWnd;
        MSG        msg;
        WNDCLASS   wc;
    
        wc.style         =  CS_HREDRAW | CS_VREDRAW;
        wc.lpfnWndProc   =  WndProc;
        wc.cbClsExtra    =  0;
        wc.cbWndExtra    =  0;
        wc.hInstance     =  hInstance;
        wc.hCursor       =  LoadCursor(NULL,IDC_ARROW);         /* Cursor laden */
        wc.hIcon         =  LoadIcon(NULL,IDI_APPLICATION);     /* Icon laden*/
        wc.hbrBackground =  (HBRUSH)GetStockObject(WHITE_BRUSH);    /* Hintergrundfarbe */
        wc.lpszClassName =  szAppName;
        wc.lpszMenuName  =  NULL;
    
        RegisterClass(&wc);
    
        hWnd = CreateWindow(szAppName,
                                szAppName,
                    WS_OVERLAPPEDWINDOW,
                                CW_USEDEFAULT,              /* X-Position auf dem Monitor(Abstand vom Rand) */
                                CW_USEDEFAULT,              /* Y-Position auf dem Monitor(Abstand vom Rand) */
                                495,                /* Fensterbreite (Objekt Größe)                 */
                                380,                /* Fensterhoehe  (Objekt Größe)                 */
                                NULL,
                                NULL,
                                hInstance,
                                NULL);
    
        ShowWindow(hWnd, iCmdShow);
        UpdateWindow(hWnd);
    
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
                DispatchMessage(&msg);
        }
        return msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        static HWND hURL, hClose, hStart, hView, hStatus, hStop;
    
        switch (message)
        {
            case WM_CREATE:
                {
                    hURL   = CreateWindowEx(0, "Edit", "www.microsoft.com", WS_CHILD | WS_VISIBLE | WS_BORDER, 
                            15, 70, 200, 20, hWnd, NULL, NULL, 0);
    
                hClose = CreateWindowEx(0, "Button", "Close" ,WS_CHILD | WS_VISIBLE | WS_BORDER, 390, 70, 80, 20, 
                            hWnd, (HMENU) NULL, NULL, 0);
    
                hStart = CreateWindowEx(0, "Button", "Start" ,WS_CHILD | WS_VISIBLE | WS_BORDER, 220, 70, 80, 20, 
                            hWnd, (HMENU) NULL, NULL, 0);
    
                hStop  = CreateWindowEx(0, "Button", "Cancel", WS_CHILD | WS_VISIBLE | WS_BORDER, 305, 70, 80, 20,
                            hWnd, (HMENU) NULL, NULL, 0);
    
                hView  = CreateWindowEx(0, "Edit", "Result", WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL |       
    
                    ES_READONLY | ES_MULTILINE, 15, 100, 455, 200, hWnd, (HMENU) NULL, NULL, 0);
    
                hStatus = CreateWindowEx(0, "STATIC", "idle", WS_CHILD | WS_VISIBLE | WS_BORDER, 15, 310, 455, 18,
                            hWnd, (HMENU) NULL, NULL, 0);
    
                return 0;
                }
    
            case WM_COMMAND:
            {
                if(lParam == (LPARAM)hClose)
                {
                    if (HIWORD(wParam) == BN_CLICKED)
                    {
                        int iAntwort = MessageBox(hWnd, "Wollen Sie das Programm wirklich beenden?",        
    
                                "Beenden?", MB_ICONINFORMATION | MB_OKCANCEL | MB_DEFBUTTON1);
    
                        if(IDOK == iAntwort) // Überprüfung
                        {
                            SendMessage(hWnd, WM_CLOSE, 0, 0);
                            }
    
                        else if(IDCANCEL == iAntwort) // Überprüfung
                        {
                            ;
                        }
                    }   
                 }          
    
                if(lParam == (LPARAM)hStart)
                {
                    if (HIWORD(wParam) == BN_CLICKED)
                    {
                        URLStart(hWnd, hURL, hStatus, hView);
                    }
                }
    
                return 0;
            }
    
            case WM_DESTROY:
                {
                    PostQuitMessage(0);
    
                    return 0;
                }
        }
    
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    
    void URLStart(HWND hWnd, HWND hURL, HWND hStatus, HWND hView)
    {
        char host_name[50];
        HINTERNET hnet, hhost, hdata;
    
        string adress;
        adress.resize(256);
    
        SendMessage(hURL, WM_GETTEXT, 50, (LPARAM)(LPCSTR)host_name); // Die URL aus der EditBox Lesen
    
        hnet  = InternetOpen("Inet",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
            hhost = InternetConnect(hnet,host_name,INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);
    
        if((hnet || hhost) == NULL)
            SendMessage(hStatus, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Error, kann nicht Verbinden!");
    
        FILE* list = fopen("string.txt","rt");
    
         while(!feof(list))
            {
                // Scan-String aus Datei lesen
                fgets((char*)adress.c_str(),adress.size(),list);
    
                if(TestFile(adress))
                SendMessage(hView, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Vorhanden");
                else
                        SendMessage(hView, WM_SETTEXT, 50, (LPARAM)(LPCSTR)"Nicht vorhanden!");
            }
    
        fclose(list);
    
        InternetCloseHandle(hdata);
        InternetCloseHandle(hhost);
            InternetCloseHandle(hnet);
    }
    
    BOOL TestFile(string path)
    {
        DWORD status = 0;
        DWORD size = sizeof(DWORD);
        BOOL result = FALSE;
    
        HINTERNET hdata = HttpOpenRequest(hhost,"HEAD",path.c_str(),NULL,NULL,NULL,0,0);
    
        if(hdata == NULL)
            return result;
    
        HttpSendRequest(hdata,NULL,0,0,0);
    
        if(HttpQueryInfo(hdata,HTTP_QUERY_STATUS_CODE,&status,&size,0))
            if(status == FILE_OK)
                result = TRUE;
    
        InternetCloseHandle(hdata);
        return result;
    }
    


  • Hat keiner eine Idee?...



  • Hmm.. das sollte eigentlich schon funktionieren, du hast ja <string> inkludiert und "using namespace std;" hast du ja auch... eigenartig...

    cya


Anmelden zum Antworten