【问题标题】:Multiple strcat is not working多个 strcat 不起作用
【发布时间】:2013-12-11 16:45:17
【问题描述】:

过去几天我一直在尝试修复此代码,但它无法正常工作..

char* appdata = getenv("APPDATA");
char* firstloglocation = strcat(appdata, "\\path\log1.txt");

这是有效的,但我需要这个:

char* appdata = getenv("APPDATA");
char* firstloglocation = strcat(appdata, "\\path\log1.txt"); 
char* secondloglocation = strcat(appdata, "\\path\log2.txt");

一旦我添加了第二行代码,它就不再做任何事情了

这两个日志都必须上传到我的 FTP 服务器,其余代码工作正常

这是原始代码:

#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


    【解决方案1】:

    strcat 修改它的第一个参数,然后返回它。因此,当您第二次调用strcat 时,appdata 将不是原始字符串。例如:

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

    另一个问题是因为第二个参数被复制到第一个参数中,appdata 必须是一个足够大的缓冲区来保存字符串。

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

    解决方案:

    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);
    

    由于您有三个字符串,因此您需要三个缓冲区。您可以使用strcpy 来避免修改appdata。然后您可以使用strcat 相应地修改您的字符串。

    【讨论】:

      猜你喜欢
      • 2013-10-16
      • 2015-10-09
      • 2015-02-20
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      相关资源
      最近更新 更多