需要使用Wininet函数的帮助
Need help using Wininet Functions
我在C++
编程,我想上传一个文件到我的web服务器,我已经使用以下代码完成:
#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;
}
但是由于这个程序将被许多人使用,如果目标文件已经存在,我无法找到一种方法来重命名上传的文件coolname.txt
,上面的代码将覆盖旧数据并重写使用2次或以上的新数据,
我试着:
- "/public_html/log.txt"+ std:: to_string (rand ())
2。A函数:-
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();
}
我然而,找到了一种方法来完成这个任务使用FtpFindFirstFile
,但我无法知道如何使用它。如果有人能给我解释得更清楚一些或者举一些例子,我将非常感激。
参考:http://msdn.microsoft.com/en-us/library/windows/desktop/aa384180(v=vs.85).aspx
您可以将输出文件名定义为变量。
#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
}
定义一个文件名作为变量,我们必须在文件名后面加上当前时间戳。
最后你的文件名将是"coolName_2014.06.25.07.08.08.txt"
- 需要帮助将结构数组传递给函数
- C++调用具有 *this 属性的单个帮助程序函数
- 用于检查值是否为其任何参数的帮助程序函数
- 无法将 2D 数组传递到 c++ 中的帮助程序函数中
- 在确定函数的时间复杂度时需要帮助
- C2783: 无法推断帮助程序函数的模板参数
- 友元函数需要一个帮助程序函数
- 在何处放置与类方法相关的帮助程序函数C++
- 需要帮助将字符串插入函数
- 从函数打印 CLI 帮助消息后,我应该如何干净地退出 C++ 程序?
- 类参数化与类和帮助程序函数混淆
- 需要有关在 C++ 中的函数 display() 上查找未定义引用的帮助
- 如何使用类中的函数从 main 打印变量,帮助理解 OOP c++
- 函数无法正常工作,调试器没有帮助
- 为什么这段代码不直接使用 C++ 幂函数?有人可以帮助我使用此代码中的电源函数吗?
- 函数和指针帮助:程序生成,但崩溃,没有任何特定错误
- 需要帮助理解 2D 数组并将其传递到函数中
- 重载模板函数帮助 - C++
- 调用模板化的成员函数:帮助我理解另一篇StackOverflow文章中的代码片段
- C++编程语法和函数帮助