如何从文件名中删除扩展名?
我想抛出文件名中的最后三个字符并获取其余字符?
我有这个代码:
char* remove(char* mystr) {
char tmp[] = {0};
unsigned int x;
for (x = 0; x < (strlen(mystr) - 3); x++)
tmp[x] = mystr[x];
return tmp;
}
I want to throw the last three character from file name and get the rest?
I have this code:
char* remove(char* mystr) {
char tmp[] = {0};
unsigned int x;
for (x = 0; x < (strlen(mystr) - 3); x++)
tmp[x] = mystr[x];
return tmp;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
尝试:
您必须自己释放返回的字符串。它只是找到字符串中最后一个
.
并将其替换为空终止符。它将通过返回 NULL 来处理错误(传递 NULL 或内存不足)。它不适用于
/this.path/is_bad
之类的东西,因为它会在非文件部分找到.
但您也可以通过执行 </
的 code>strrchr 或任何路径分隔符,并确保其位置为NULL
或在.
位置之前。这个问题的更通用的解决方案可能是:
这会产生:
Try:
You'll have to free the returned string yourself. It simply finds the last
.
in the string and replaces it with a null terminator character. It will handle errors (passingNULL
or running out of memory) by returningNULL
.It won't work with things like
/this.path/is_bad
since it will find the.
in the non-file portion but you could handle this by also doing astrrchr
of/
, or whatever your path separator is, and ensuring it's position isNULL
or before the.
position.A more general purpose solution to this problem could be:
and this produces:
使用rindex来定位“.”特点。如果字符串是可写的,则可以将其替换为字符串终止符 char ('\0'),然后就完成了。
注意:更高版本的 gcc 以及其他编译器可能无法识别这一点。相反,应使用
#include string.h
代替 strings.h,并使用strrchr
。Use rindex to locate the "." character. If the string is writable, you can replace it with the string terminator char ('\0') and you're done.
NOTE: This will not be recognized by later versions of gcc, and, probably, other compilers. Instead,
#include string.h
instead of strings.h, and usestrrchr
.如果您实际上只想删除最后三个字符,因为您以某种方式知道您的文件名的扩展名正好是三个字符长(并且您想保留点):
或者让调用者提供目标缓冲区(他们必须确保足够长):
如果您想一般性地删除文件扩展名,那就更难了,通常应该使用您的平台提供的任何文件名处理例程(POSIX 上的
basename
,_wsplitpath_s
Windows)如果您有可能处理路径而不仅仅是文件名的最后部分:想一想,您可能想要传递
dst+1
而不是dst
到 strrchr,因为以点开头的文件名可能不应该被截断为仅“.”。取决于它的用途。If you literally just want to remove the last three characters, because you somehow know that your filename has an extension exactly three chars long (and you want to keep the dot):
Or let the caller provide the destination buffer (which they must ensure is long enough):
If you want to generically remove a file extension, that's harder, and should normally use whatever filename-handling routines your platform provides (
basename
on POSIX,_wsplitpath_s
on Windows) if there's any chance that you're dealing with a path rather than just the final part of the filename:Come to think of it, you might want to pass
dst+1
rather thandst
to strrchr, since a filename starting with a dot maybe shouldn't be truncated to just ".". Depends what it's for.我会尝试以下算法:
I would try the following algorithm:
只需将点替换为“0”即可。如果你知道你的扩展名总是 3 个字符长,你可以这样做:
这将输出“test”。另外,您不应该返回指向局部变量的指针。编译器也会对此发出警告。
Just replace the dot with "0". If you know that your extension is always 3 characters long you can just do:
This will output "test". Also, you shouldn't return a pointer to a local variable. The compiler will also warn you about this.
为了让 paxdiablo 的第二个更通用的解决方案在 C++ 编译器中工作,我将这一行更改
为
:希望这对某人有帮助。
To get paxdiablo's second more general purpose solution to work in a C++ compiler I changed this line:
to:
Hope this helps someone.
这应该可以完成这项工作:
This should do the job:
获取位置并将该位置复制到新的 char * 中。
Get location and just copy up to that location into a new char *.
这是更改扩展名的简单方法。
This is simple way to change extension name.
具有可配置的最小文件长度和可配置的最大扩展长度。返回扩展名更改为空字符的索引,如果未找到扩展名,则返回 -1。
With configurable minimum file length and configurable maximum extension length. Returns index where extension was changed to null character, or -1 if no extension was found.
我使用这段代码:
它正确处理 Windows 路径约定(
/
和\
都可以是路径分隔符)。I use this code:
It handles the Windows path convention correctly (both
/
and\
can be path separators).