将文件复制到windir c

时间:2016-06-02 22:33:49

标签: c arrays string tchar

我需要将一个文件复制到windir,所以我这样做:

TCHAR windir[MAX_PATH];
GetWindowsDirectory(windir, MAX_PATH);

得到windir。

char newLocation[]="text.txt"; // how to add the windir here?
BOOL stats=0;
CopyFile(filename, newLocation, stats);

我的问题是,我想在char newLocation []中的text.txt之前添加windir值。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

您是否尝试过串联字符串?

#include <stdlib.h>
#include <string.h>

char* concat(char *s1, char *s2)
{
    char *result = malloc(strlen(s1)+strlen(s2)+1);//+1 for the zero-terminator
    //in real code you would check for errors in malloc here
    strcpy(result, s1);
    strcat(result, s2);
    return result;
}

如果这不起作用,请给wcscat()一个机会!

来源:

  1. How do I concatenate two strings in C?
  2. What is difference between TCHAR and WCHAR?
  3. C++ Combine 2 Tchar