【发布时间】:2011-01-14 04:01:27
【问题描述】:
我有一个 char 数组:
char* name = "hello";
我想为该名称添加扩展名
hello.txt
我该怎么做?
name += ".txt" 不起作用
【问题讨论】:
我有一个 char 数组:
char* name = "hello";
我想为该名称添加扩展名
hello.txt
我该怎么做?
name += ".txt" 不起作用
【问题讨论】:
看看strcat函数。
特别是,你可以试试这个:
const char* name = "hello";
const char* extension = ".txt";
char* name_with_extension;
name_with_extension = malloc(strlen(name)+1+4); /* make space for the new string (should check the return value ...) */
strcpy(name_with_extension, name); /* copy name into the new var */
strcat(name_with_extension, extension); /* add the extension */
【讨论】:
const char *name, *extension吗?字符串文字不是 char *.
sizeof(char) 不使用实际字节数?例如malloc(strlen(name) + 4 * sizeof(char) + 1)
我有一个字符数组:
char* name = "hello";
不,您有一个指向string literal 的字符指针。在许多用法中,您可以添加 const 修饰符,具体取决于您对 name 指向的内容或字符串值“hello”更感兴趣。您不应该尝试修改文字(“hello”),因为bad things can happen。
要传达的主要内容是 C 没有适当的(或 一等)字符串类型。 “字符串”通常是带有终止空('\0' 或十进制 0)字符的字符(字符)数组,以表示字符串的结尾,或指向字符数组的指针。
我建议阅读字符数组,The C Programming Language(第 28 页第二版)第 1.9 节。我强烈推荐阅读这本小书(
关于您的问题,C FAQ 的第 6 节 - Arrays and Pointers 和第 8 节 - Characters and Strings 可能会有所帮助。问题6.5 和8.4 可能是很好的起点。
我希望这可以帮助您理解为什么您的摘录不起作用。其他人已经概述了需要进行哪些更改才能使其发挥作用。基本上,您需要一个足够大的 char 数组(字符数组)来存储带有终止(结束)'\0' 字符的整个字符串。然后您可以使用标准 C 库函数 strcpy(或更好的 strncpy)将“Hello”复制到其中,然后您想使用标准 C 库 strcat(或更好的 strncat)函数进行连接。您将需要包含 string.h 头文件来声明函数声明。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
char filename[128];
char* name = "hello";
char* extension = ".txt";
if (sizeof(filename) < strlen(name) + 1 ) { /* +1 is for null character */
fprintf(stderr, "Name '%s' is too long\n", name);
return EXIT_FAILURE;
}
strncpy(filename, name, sizeof(filename));
if (sizeof(filename) < (strlen(filename) + strlen(extension) + 1) ) {
fprintf(stderr, "Final size of filename is too long!\n");
return EXIT_FAILURE;
}
strncat(filename, extension, (sizeof(filename) - strlen(filename)) );
printf("Filename is %s\n", filename);
return EXIT_SUCCESS;
}
【讨论】:
asprintf 不是 100% 标准的,但它可以通过 GNU 和 BSD 标准 C 库获得,因此您可能拥有它。它分配输出,因此您不必坐在那里数字符。
char *hi="Hello";
char *ext = ".txt";
char *cat;
asprintf(&cat, "%s%s", hi, ext);
【讨论】:
您可以在此处复制并粘贴答案,或者您可以阅读我们的主持人 Joel 对strcat 的评价。
【讨论】:
您可以使用 sprintf() 函数连接字符串。在你的情况下,例如:
char file[80];
sprintf(file,"%s%s",name,extension);
你将在“文件”中结束连接字符串。
【讨论】:
在极少数情况下,您无法使用 strncat、strcat 或 strcpy。而您无权访问<string.h>,因此您无法使用strlen。此外,您可能甚至不知道 char 数组的大小,并且您仍然想连接,因为您只有指针。好吧,你可以做老派 malloc 并像自己一样计算字符..
char *combineStrings(char* inputA, char* inputB) {
size_t len = 0, lenB = 0;
while(inputA[len] != '\0') len++;
while(inputB[lenB] != '\0') lenB++;
char* output = malloc(len+lenB);
sprintf((char*)output,"%s%s",inputA,inputB);
return output;
}
它只需要#include <stdio.h>,您很可能已经包含它
【讨论】:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *name = "hello";
int main(void) {
char *ext = ".txt";
int len = strlen(name) + strlen(ext) + 1;
char *n2 = malloc(len);
char *n2a = malloc(len);
if (n2 == NULL || n2a == NULL)
abort();
strlcpy(n2, name, len);
strlcat(n2, ext, len);
printf("%s\n", n2);
/* or for conforming C99 ... */
strncpy(n2a, name, len);
strncat(n2a, ext, len - strlen(n2a));
printf("%s\n", n2a);
return 0; // this exits, otherwise free n2 && n2a
}
【讨论】: