我正在使用Linux GCC c99。
我想知道什么是最好的技术。要更改字符串。我正在使用strstr();
我有一个名为“file.vce”的文件名,我想将扩展名更改为“file.wav”。
这是最好的方法吗?
char file_name[80] = "filename.vce";
char *new_file_name = NULL;
new_file_name = strstr(file_name, "vce");
strncpy(new_file_name, "wav", 3);
printf("new file name: %s\n", new_file_name);
printf("file name: %s\n", file_name);
非常感谢任何建议,
我已根据您的建议编辑了我的答案。你能看到别的错吗?
/** Replace the file extension .vce with .wav */
void replace_file_extension(char *file_name)
{
char *p_extension;
/** Find the extension .vce */
p_extension = strrchr(file_name, '.');
if(p_extension)
{
strcpy(++p_extension, "wav");
}
else
{
/** Filename did not have the .vce extension */
/** Display debug information */
}
}
int main(void)
{
char filename[80] = "filename.vce";
replace_file_extension(filename);
printf("filename: %s\n", filename);
return 0;
}
答案 0 :(得分:3)
有一些问题:
char file_name[80] = "filename.vce";
char *new_file_name = NULL;
new_file_name = strstr(file_name, "vce");
strncpy(new_file_name, "wav", 3);
printf("new file name: %s\n", new_file_name);
printf("file name: %s\n", file_name);
只有一个字符串的存储空间,但最后你会尝试打印两个不同的字符串。
名为new_file_name
的变量实际上指向同一文件名的一部分。
字符串vce可能出现在文件名中的任何位置,而不仅仅是扩展名。如果文件名是srvce.vce
?
你可能想找到最后一个。字符串中的字符,然后检查它是否后跟预期的扩展名,然后替换该扩展名。请记住,如果您通过修改原始缓冲区来执行此操作,则之后将无法打印旧字符串。
答案 1 :(得分:2)
而不是搜索。或者vce中的任何一个可能在字符串中出现多次,计算字符串的长度并减去3以指向扩展名。使用strncpy就地替换扩展。
size_t length;
char* pextension;
char file_name[80] = "filename.vce";
printf("file name: %s\n", file_name);
length = strlen(file_name);
pextension = file_name + length - 3;
strncpy(pextension, "wav", 3);
printf("new file name: %s\n", file_name);
答案 2 :(得分:1)
您必须在new_file_name
末尾手动添加零终结符,因为strncpy()
不会在您的情况下添加它。
恰好你已经在正确的地方有零字节,但在所有情况下都无法保证,所以这是一个习惯,比如
new_file_name[3] = '\0';
在strncpy()
之后。
还要注意字符串“vce”可能出现在文件名中。
答案 3 :(得分:1)
您的代码存在一些问题。这是一个不同的看法,更改内联更改:
char file_name[80] = "filename.vce";
char *new_file_name = NULL;
printf("old file name: '%s'\n", file_name);
/* Use strrstr(), to find the last occurance, and include the period. */
new_file_name = strrstr(file_name, ".vce");
if(new_file_name != NULL)
{
/* Use plain strcpy() to make sure the terminator gets there.
* Don't forget the period.
*/
strcpy(new_file_name, ".wav");
}
printf("new file name: '%s'\n", file_name);
这仍然可以改进,例如,它不会检查是否有足够的空间来包含新扩展名。但它确实以一种比单个字符更为自然的方式终止字符串。
答案 4 :(得分:1)
我会这样做
char file_name[80] = "filename.vce";
char *pExt;
pExt = strrchr(file_name, ".");
if( pExt )
strcpy(++pExt, "wav");
else
{
// hey no extension
}
printf("file name: %s\n", file_name);
你需要在每个c程序中进行指针操作。当然,您还需要检查缓冲区运行等,甚至使用特定于路径的函数。
答案 5 :(得分:1)
我很无聊,而不是在原始代码中指出问题,我写了自己的。为了达到指导的目的,我试图说清楚。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Replace the last suffix in a filename with a new suffix. Copy the new
name to a new string, allocated with malloc. Return new string.
Caller MUST free new string.
If old name has no suffix, a period and the new suffix is appended
to it. The new suffix MUST include a period it one is desired.
Slashes are interepreted to separate directories in the filename.
Suffixes are only looked after the last slash, if any.
*/
char *replace_filename_suffix(const char *pathname, const char *new_suffix)
{
size_t new_size;
size_t pathname_len;
size_t suffix_len;
size_t before_suffix;
char *last_slash;
char *last_period;
char *new_name;
/* Allocate enough memory for the resulting string. We allocate enough
for the worst case, for simplicity. */
pathname_len = strlen(pathname);
suffix_len = strlen(new_suffix);
new_size = pathname_len + suffix_len + 1;
new_name = malloc(new_size);
if (new_name == NULL)
return NULL;
/* Compute the number of characters to copy from the old name. */
last_slash = strrchr(pathname, '/');
last_period = strrchr(pathname, '.');
if (last_period && (!last_slash || last_period > last_slash))
before_suffix = last_period - pathname;
else
before_suffix = pathname_len;
/* Copy over the stuff before the old suffix. Then append a period
and the new suffix. */
#if USE_SPRINTF
/* This uses snprintf, which is how I would normally do this. The
%.*s formatting directive is used to copy a specific amount
of text from pathname. Note that this has the theoretical
problem with filenames larger than will fit into an integer. */
snprintf(new_name, new_size, "%.*s%s", (int) before_suffix, pathname,
new_suffix);
#else
/* This uses memcpy and strcpy, to demonstrate how they might be
used instead. Much C string processing needs to be done with
these low-level tools. */
memcpy(new_name, pathname, before_suffix);
strcpy(new_name + before_suffix, new_suffix);
#endif
/* All done. */
return new_name;
}
int main(int argc, char **argv)
{
int i;
char *new_name;
for (i = 1; i + 1 < argc; ++i) {
new_name = replace_filename_suffix(argv[i], argv[i+1]);
if (new_name == NULL) {
perror("replace_filename_suffix");
return EXIT_FAILURE;
}
printf("original: %s\nsuffix: %s\nnew name: %s\n",
argv[i], argv[i+1], new_name);
free(new_name);
}
return 0;
}