Problem mit der while Schleife



  • Original erstellt von <eonic+d>:
    **Gibt das Gleiche in grün

    Ausgabe:
    Datei nicht gefunden! Datei nicht gefunden! Datei gefunden!**

    Zeig doch mal bitte den Code wo du das drinhast, ich kann mir das nicht recht vorstellen.



  • #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <wininet.h>
    #include <iostream>
    #include <fstream.h>
    #include <string>
    using namespace std;

    const DWORD FILE_OK = 3158066;
    HINTERNET hnet, hhost, hdata;
    DWORD status = 0;
    DWORD size = sizeof(DWORD);

    char buffer[80];

    int main()
    {

    hnet = InternetOpen("Inet",INTERNET_OPEN_TYPE_DIRECT,NULL,NULL,0);
    hhost = InternetConnect(hnet,"www.c-plusplus.net",INTERNET_DEFAULT_HTTP_PORT,NULL,NULL,INTERNET_SERVICE_HTTP,0,0);

    ifstream file;
    file.open("string.dat", ios::in | ios::binary);

    if (!file.is_open())
    cout << "Error opening file";

    while (!file.eof())
    {
    BOOL result = FALSE; // Ist doch richtig, oder?
    file.getline(buffer,80);

    hdata = HttpOpenRequest(hhost,"HEAD",buffer,NULL,NULL,NULL,0,0);

    if(hdata == NULL)
    result = FALSE;

    HttpSendRequest(hdata,NULL,0,0,0);

    if(HttpQueryInfo(hdata,HTTP_QUERY_STATUS_CODE,&status,&size,0))
    if(status == FILE_OK)
    result = TRUE;

    if(result == TRUE)
    cout << "Datei vorhanden!";
    else{
    cout << "Datei nicht vorhanden!"; result = FALSE;}

    }

    file.close();

    getchar();

    InternetCloseHandle(hdata);
    InternetCloseHandle(hhost);
    InternetCloseHandle(hnet);

    return 0;
    }



  • Sorry da weiß ich wirklich nicht weiter.



  • Probier mal normale Strings aus, die nicht aus der Datei kommen, vielleicht stimmt was am Einlesen der Datei nicht.

    cya



  • Wie würdet ihr es denn machen, wenn ihr Strings aus einer Text Datei einlesen und per HttpOpenRequest einzeln überprüfen müsstet?



  • zu spät gesehen...

    @Magic

    das habe ich schon gemacht und da funzt es, nur weil es sehr viele später einmal werden sollen, wollte ich die in einer Datei anlegen



  • Also wenn du Strings nimmst funzt es? Probier mal nen String-Array in deiner while-Schleife. Wenn das dann auch noch funzt, dann stimmt was mit dem Auslesen nicht.

    cya



  • Weiss jetzt nicht ob ich das jetzt so richtig verstehe, denn die while Schleife hab ich nur deswegen eingeführt wegen dem Datei Handling, denn als ich das mit den Strings gemacht habe, hatte ich eine for Schleife verfendet.

    Den Code hatte ich aber auch nur von Kane bekommen:

    #define WIN32_LEAN_AND_MEAN
    
    #include <windows.h>
    #include <wininet.h> //wininet.lib linken
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    const DWORD FILE_OK = 3158066;
    
    bool TestFile(string path);
    HINTERNET hnet,hhost;
    
    int main()
    {
        char host[50];
        string files[2] = {{"index.asp"}, {"home.asp"}};
    
        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";
    
        for(int i=0; i<2; i++)
        {
            if(TestFile(files[i]))
            {
                cout << "File: " << files[i] << "\t[   Ok   ]\n";
            }
            else
            {
                cout << "File: " << files[i] << "\t\t[ Failed ]\n";
            }
        }
    
        getchar();
    
        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; // Datei ok
    
        InternetCloseHandle(hdata);
        return result;
    }
    


  • 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