Comum
Comum

Reputation: 453

FtpPutFile keeps not sending file

I've written an FTP client in C++ to send files to an FTP server automatically. However I have an error when I try to use the function FtpPutFile(). Everytime I use it I get error 2 from the function GetLastError() and no error from perror(). I know it's not because of the firewall as I can do it manually from my command prompt. Also, it's not because I can't connect to the ftp server, as I have checked for errors in this part of the code.

I am also developing this in a Visual Studio 2010 environment and I have set my Character Set to Not Set so I can use a string.c_str().

So the only thing that can be wrong is how I call the function itself. The next part is my code that refers to the function that is suposed to send a file through FTP:

void fileTransfer(const std::string& filename)
{
    LPCTSTR pstrServer = TEXT("ip.address");
    LPCTSTR pstrUserName = TEXT("user");
    LPCTSTR pstrPassword = TEXT("pass");
    LPCTSTR lpszLocalFile = (LPCTSTR)filename.c_str();
    LPCTSTR lpszNewRemoteFile = (LPCTSTR)filename.c_str();

    HINTERNET hSession = InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
    HINTERNET hService = InternetConnect(hSession, pstrServer,    INTERNET_DEFAULT_FTP_PORT, pstrUserName, pstrPassword, INTERNET_SERVICE_FTP, 0, 0);

   if(FtpPutFile(hService, filename.c_str(), filename.c_str(), FTP_TRANSFER_TYPE_ASCII, 0))
   {
        printf("File copied\n");
   }
   else
   {
        printf("File not copied\n");
        std::cerr << "Error: " << GetLastError() << std::endl;
        perror( "Error transfering file" );
        putchar('\n');
   }
   InternetCloseHandle(hService);
   InternetCloseHandle(hSession);
}

Does anyone know what might be wrong here? Because this code only runs if a certain type of file .txt with a certain structure name, is found. But in the FtpPutFile() it says it is not found. Even if I print filename.c_str() I get the proper file name.

Upvotes: 0

Views: 1255

Answers (1)

gjacobs
gjacobs

Reputation: 133

GetLastError 2 means "file not found".

I would check the contents of your source and destination parameters in your FtpPutFile statement.

Upvotes: 1

Related Questions