Note: Follow Alk's solution as a guide on your implemetation
我想创建一个函数,让用户插入一个用于保存输出文件的目录。我的输出文件将有一个静态名称,所以我只需要路径。
我想从用户那里读取路径并将其附加到输出文件的名称之前。所以它会是这样的:
输出名称(由另一个函数生成)outLogFile = "outLogFile.log"
user input = D:\Datasets\some_folder\more_folders
RESULT = D:\Datasets\some_folder\more_folders\outLogFile.log
我这样做的方式是,我在临时文件中插入输出名称,使用 strcpy
将文件路径复制到 outLogFile 中,并使用 strcat
将临时文件附加到 outLogFile 中。
有没有更简单的方法呢?一种不使用临时文件将两个字符串合并到我的 outLogFile 中的方法?复制 ouLogFile 字符串之前的 path_file 字符串,并将其保存到 outLogFile 中的单个命令?
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char user_input[100], *path_file,*temp;
char *outLogFile = "outLogFile.log";
printf("Filepath:\n (!Do not inlude filename!)\n");
gets(user_input);
path_file = (char*)malloc(strlen(user_input)+1);
if (user_input[strlen(user_input) - 1]!='\\')
{
strcpy(path_file, user_input);
strcat(path_file, "\\");
}
else
{
strcpy(path_file, user_input);
}
temp = outLogFile;
strcpy(outLogFile, path_file);
strcat(outLogFile, temp);
printf("%s\n%s\n", path_file,outLogFile);
system("pause");
return 0;
}
编辑:我可以使用user_input
和path_file
到malloc
outLogFile
和strcpy
strcat
字符串如下
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char user_input[100];
char *outLogFile;
char *path_file = "outLogFile.log";
printf("Filepath:\n (!Do not inlude filename!)\n");
fgets(user_input,sizeof(user_input), stdin);
printf("%c\n",user_input[strlen(user_input) - 1]);
outLogFile = (char*)malloc(strlen(user_input)+strlen(path_file));
if (user_input[strlen(user_input) - 1]!='\\')
{
strcpy(outLogFile,user_input);
strcat(outLogFile, "\\");
strcat(outLogFile,path_file);
}
else
{
strcpy(outLogFile,user_input);
strcat(outLogFile,path_file);
}
printf("%s",outLogFile);
system("pause");
return 0;
}
但是这段代码通过点击返回按钮获取\n 并将其插入到两个字符串之间
最佳答案
要在一个字符串前添加另一个字符串并将结果存储在一个新字符串中,最灵活的通用方法是使用动态内存分配,如下所示:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(void)
{
char * ps1 = "Hello";
char * ps2 = " World";
size_t length_total = strlen(ps1) + strlen(ps2);
char * ps3 = malloc((length_total + 1) * sizeof *ps3); /* One more for
the 0-terminator. */
if (NULL == ps3)
{
perror("malloc() failed");
exit(EXIT_FAILURE);
}
strcpy(ps3, ps1);
strcat(ps3, ps2);
/* Use ps3. */
puts(ps3);
/* Clean up. */
free(ps3);
return EXIT_SUCCESS;
}
在您的特定情况下,代码提供了一个没有路径的默认文件名,并且用例是允许在运行时为文件名添加前缀,您可能会以这样一种更简单的方式来解决这个问题。
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#define LOGFILENAME "some.log"
int main(void)
{
char logfilepath[PATH_MAX] = LOGFILENAME; /* Just to make sure. */
char dir[PATH_MAX] = "";
if (NULL == fgets(dir, sizeof dir, stdin))
{
if (ferror(stdin))
{
perror("fgets() failed");
exit(EXIT_FAILURE);
}
}
dir[strcspn(dir, "\n\r")] = 0;
{
size_t length_dir = strlen(dir);
if (length_dir > 0 && '/' != dir[length_dir - 1])
{
if (PATH_MAX < length_dir)
{
errno = EINVAL;
perror("'dir' to long");
exit(EXIT_FAILURE);
}
strcat(dir, "/");
++length_dir;
}
{
size_t length_total = length_dir + strlen(logfilepath);
if (PATH_MAX < length_total)
{
errno = EINVAL;
perror("'dir/filename' to long");
exit(EXIT_FAILURE);
}
}
}
strcpy(logfilepath, dir);
strcat(logfilepath, LOGFILENAME);
/* Use logfilepath, . */
puts(logfilepath);
return EXIT_SUCCESS;
}
为了不使用 #define
欺骗它并且不使用第三个变量去移动:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
char logfilepath[PATH_MAX] = "some.log";
{
char dir[PATH_MAX] = "";
if (NULL == fgets(dir, sizeof dir, stdin))
{
if (ferror(stdin))
{
perror("fgets() failed");
exit(EXIT_FAILURE);
}
}
dir[strcspn(dir, "\n\r")] = 0;
{
size_t length_filepath = strlen(logfilepath);
size_t length_dir = strlen(dir);
if (length_dir > 0 && '/' != dir[length_dir - 1])
{
if (PATH_MAX < length_dir)
{
errno = EINVAL;
perror("'dir' to long");
exit(EXIT_FAILURE);
}
strcat(dir, "/");
++length_dir;
}
if (PATH_MAX < (length_dir + length_filepath))
{
errno = EINVAL;
perror("'dir/filename' to long");
exit(EXIT_FAILURE);
}
memmove(logfilepath + length_dir, logfilepath, length_filepath + 1);
memcpy(logfilepath, dir, length_dir);
}
}
/* Use logfilepath, . */
puts(logfilepath);
return EXIT_SUCCESS;
}
关于在C中复制和合并两个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43585456/