需要帮助使用Wininet函数 [英] Need help using Wininet Functions
问题描述
我在 C ++
中进行编程,并且要将文件上传到我已使用以下代码完成的Web服务器:
#includestdafx.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,Lftp.freetzi.com,INTERNET_DEFAULT_FTP_PORT,Lmyuser.freetzi.com,mypassword,INTERNET_SERVICE_FTP,0,0);
FtpPutFile(hFtp,LC:\\deneme.txt,LcoolName.txt,FTP_TRANSFER_TYPE_BINARY,0);
if(FtpPutFile(hFtp,LC:\\ desteme.txt,LcoolName.txt,FTP_TRANSFER_TYPE_BINARY,0))
{
MessageBox NULL,LUpload Successful。,LTitle,NULL);
}
else
{
//这是我得到的消息...
MessageBox(NULL,L上传失败,L标题空值);
}
InternetCloseHandle(hFtp);
InternetCloseHandle(hInternet);
return 0;
}
但是由于这个程序将被很多人使用,如果目标文件已经存在,则重命名上传的文件 coolname.txt
的方式,上面的代码将覆盖旧数据并重写已使用的新数据2次或更多次,
我试过:
- / public_html / log.txt+ std :: to_string(rand())
2。函数: -
generateFileName(std :: string const& directory,
std :: string const& rootName,
std :: string const& extension)
{
std :: ostringstream results;
results.fill('0');
results<<目录<< '/'<< rootName<< ' - '<< std :: setw(4)<< rand()<<延期;
return result.str();
}
但是,我发现一种方法可以使用 FtpFindFirstFile
但我无法知道如何使用它。如果有人可以解释我更清楚或给一些例子,我真的appriciate。
参考: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384180(v = vs.85).aspx
$ b<$ c $ c> #include< iostream>
#include< string>
#include< stdio.h>
#include< time.h>
//获取当前日期/时间,格式为YYYY-MM-DD.HH:mm:ss
const std :: string currentDateTime(){
time_t now = time (0)。
struct tm tstruct;
char buf [80];
tstruct = * localtime(& now);
//访问http://en.cppreference.com/w/cpp/chrono/c/strftime
//有关日期/时间格式的更多信息
strftime(buf,sizeof( %Y-%m-%d。%X,& tstruct);
return buf;
}
int main(){
std :: cout< currentDateTime()=< currentDateTime()<< std :: endl;
getchar(); //等待键盘输入
}
定义一个文件名作为变量,
最后,您的文件名将是coolName_ 2014.06.25.07.08.08 .txt
我希望这会帮助你。
I am programming in C++
and I want to upload a file to my web server which i have already done using the following code:
#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;
}
But as this program will be used by many people, I can't find a way to rename the uploaded file coolname.txt
if the target file already exists, the code above will overwrite the old data and rewrite the new one of used 2 or more times,
I tried:
- "/public_html/log.txt" + std::to_string(rand())
2 . A function :-
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();
}
I however, found a way to accomplish this task using FtpFindFirstFile
But I am unable to know how to use it. If someone could explain me more clear or give some examples, I would really appriciate.
Reference : http://msdn.microsoft.com/en-us/library/windows/desktop/aa384180(v=vs.85).aspx
you can define the Output File name as 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
}
Define a file name as variable , in which we have to append current timestamp with filename.
Finally your file name will be "coolName_2014.06.25.07.08.08.txt"
I hope this will help you.
这篇关于需要帮助使用Wininet函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!