Pobieranie pliku z FTP

0

Programuje w Code::Block.
Kod:

#include <Windows.h>
#include <Wininet.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <tlhelp32.h>
#include <string>
#include <process.h>
#include <vector>
#include <string>
#include <fstream>

#pragma comment(lib,"Wininet.lib")

using namespace std;

HINTERNET netstart()
{
    const HINTERNET handle = InternetOpenW( 0, INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0 );
    if( handle == 0 )
    {
        const DWORD error = GetLastError();
        cerr << "InternetOpen(): " << error << "." << endl;
    }
    return( handle );
}

void netclose( HINTERNET object )
{
    const BOOL result = InternetCloseHandle( object );
    if( result == FALSE )
    {
        const DWORD error = GetLastError();
        cerr << "InternetClose(): " << error << "." << endl;
    }
}

HINTERNET netopen( HINTERNET session, LPCWSTR url )
{
    const HINTERNET handle = InternetOpenUrlW( session, url, 0, 0, 0, 0 );
    if( handle == 0 )
    {
        const DWORD error = GetLastError();
        cerr << "InternetOpenUrl(): " << error << "." << endl;
    }
    return( handle );
}

void netfetch( HINTERNET istream, ostream & ostream )
{
    static const DWORD SIZE = 1024;
    DWORD error = ERROR_SUCCESS;
    BYTE data[ SIZE ];
    DWORD size = 0;
    do
    {
        BOOL result = InternetReadFile( istream, data, SIZE, & size );
        if( result == FALSE )
        {
            error = GetLastError();
            cerr << "InternetReadFile(): " << error << "." << endl;
        }
        ostream.write(( const char * ) data, size );
    }
    while(( error == ERROR_SUCCESS ) &&( size > 0 ) );

}


int Download( WCHAR * url, string path )
{
    const HINTERNET session = netstart();
    if( session != 0 )
    {
        const HINTERNET istream = netopen( session, url );
        if( istream != 0 )
        {
            ofstream ostream( path.c_str(), ios::binary );
            if( ostream.is_open() )
            {
                netfetch( istream, ostream );
            }
            else
            {
                return 1;
            }
            netclose( istream );
        }
        netclose( session );
    }
    return 0;
}

std::wstring StringToWString( const std::string s )
{
    std::wstring temp( s.length(), L' ' );
    std::copy( s.begin(), s.end(), temp.begin() );
    return temp;
}

int main( int argc, char * argv[] )
{
    if( argc != 3 ) return 1;

    string fname = argv[ 1 ];
    string fs = argv[ 2 ];
    Download(( WCHAR * ) StringToWString( fname ).c_str(), fs.c_str() );
    return 0;
}

Co wyrzuca mi błędy:

C:\Users\User\Documents\Cpp\Projekty\FTP Download\main.cpp|13|warning: ignoring #pragma comment |
obj\Release\main.o:main.cpp|| undefined reference to InternetReadFile@16'| obj\Release\main.o:main.cpp|| undefined reference to InternetCloseHandle@4'|
obj\Release\main.o:main.cpp|| undefined reference to InternetOpenUrlW@24'| obj\Release\main.o:main.cpp|| undefined reference to InternetOpenW@20'|
||=== Build finished: 4 errors, 1 warnings ===|

O co chodzi ???
Program ma za zadanie wywołany z wiersza poleceń np. "FTP_download.exe www.google.com C:\Users\User\Desktop\plik" pobrać plik do wybranej sciezki HELP

1
C:\Users\User\Documents\Cpp\Projekty\FTP Download\main.cpp|13|warning: ignoring #pragma comment

Kompilator GCC nie obsługuje #pragma comment lib.
Musisz dodać bibliotekę do opcji linkera (zapewne w postaci -lwininet)

0

Dziękuję już wszystko działa :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1