c++ function upload ftp wininet

c++ - Necesita ayuda para usar las funciones de Wininet



function upload (1)

Estoy programando en C++ y quiero subir un archivo a mi servidor web que ya he hecho usando el siguiente código:

#include "stdafx.h" #include <windows.h> #include <wininet.h> #pragma comment(lib, "wininet") int _tmain(int argc, _TCHAR* argv[]) { HINTERNET hInternet; HINTERNET hFtp; hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL,NULL,0); hFtp = InternetConnect(hInternet, L"ftp.freetzi.com", INTERNET_DEFAULT_FTP_PORT, L"myuser.freetzi.com", "mypassword", INTERNET_SERVICE_FTP, 0, 0); FtpPutFile(hFtp, L"C://deneme.txt", L"coolName.txt", FTP_TRANSFER_TYPE_BINARY, 0); if ( FtpPutFile(hFtp, L"C://deneme.txt", L"coolName.txt", FTP_TRANSFER_TYPE_BINARY, 0) ) { MessageBox(NULL, L"Upload Successful.", L"Title", NULL); } else { //This is the message I get... MessageBox(NULL, L"Upload Failed.", L"Title", NULL); } InternetCloseHandle(hFtp); InternetCloseHandle(hInternet); return 0; }

Pero como este programa será utilizado por muchas personas, no puedo encontrar la manera de cambiar el nombre del archivo cargado coolname.txt si el archivo de destino ya existe, el código anterior sobrescribirá los datos anteriores y reescribirá el nuevo usado de 2 o mas veces,

Lo intenté:

  1. "/public_html/log.txt" + std :: to_string (rand ())

2. Una función :-

generateFileName( std::string const& directory, std::string const& rootName, std::string const& extension ) { std::ostringstream results; results.fill( ''0'' ); results << directory << ''/'' << rootName << ''-'' << std::setw(4) << rand() << extension; return results.str(); }

Sin embargo, encontré una forma de lograr esta tarea usando FtpFindFirstFile pero no puedo saber cómo usarlo. Si alguien pudiera explicarme más claramente o dar algunos ejemplos, realmente lo haría.

Referencia: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384180(v=vs.85).aspx


puede definir el nombre del archivo de salida como variable.

#include <iostream> #include <string> #include <stdio.h> #include <time.h> // Get current date/time, format is YYYY-MM-DD.HH:mm:ss const std::string currentDateTime() { time_t now = time(0); struct tm tstruct; char buf[80]; tstruct = *localtime(&now); // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime // for more information about date/time format strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct); return buf; } int main() { std::cout << "currentDateTime()=" << currentDateTime() << std::endl; getchar(); // wait for keyboard input }

Defina un nombre de archivo como variable, en el cual tenemos que anexar la marca de tiempo actual con el nombre del archivo.

Finalmente su nombre de archivo será "coolName_ 2014.06.25.07.08.08 .txt"

Espero que esto ayude.