Cannot upload a file to a ftp server using C++
I have this simple code to upload a file to a server, but it seems that it开发者_Python百科 doesnt work, doesn't upload any file(FtpPutFile returns 0). I am using FileZilla Server and this is my code and what FileZilla says:
void upload()
{
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hFtpSession = InternetConnect(hInternet,"127.0.0.1",INTERNET_DEFAULT_FTP_PORT,"vbx","pass",INTERNET_SERVICE_FTP, 0,0 );
FtpPutFile(hFtpSession, "c:\\stories.txt", "e:\\text.txt", FTP_TRANSFER_TYPE_BINARY, 0);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
}
(000011)3/27/2011 0:01:53 AM - (not logged in) (127.0.0.1)> USER vbx
(000011)3/27/2011 0:01:53 AM - (not logged in) (127.0.0.1)> 331 Password required for vbx
(000011)3/27/2011 0:01:53 AM - (not logged in) (127.0.0.1)> PASS *******
(000011)3/27/2011 0:01:53 AM - vbx (127.0.0.1)> 230 Logged on
(000011)3/27/2011 0:01:53 AM - vbx (127.0.0.1)> disconnected.
Thank you.
edit: GetLastError() returns: The process cannot access the file because it is being used by another process.
With GetLastError() returning ERROR_SHARING_VIOLATION (32) for FtpPutFile, it likely means that that there is an open handle to "c:\stories.txt" that prevents read sharing. If you have this file open in your program, you will need to either allow read sharing in the CreateFile call or close all open handles that prevent sharing so that FtpPutFile can open the file.
精彩评论