提取文件扩展名 [英] Extracting the extension of a file
问题描述
一些细节
- 语言:C
- 系统:Linux;使用命令行(终端), 通过终端 读取文件
- 使用C的用户体验:3 个月
- Language: C
- System: Linux; working with command line (terminal), files are read in through the terminal
- User experience with C: 3 months
我一直在尝试提取给定文件的扩展名,例如"myfile.wld",以便稍后我可以检查是否在终端上输入了正确类型的文件,然后再进行操作.文件内容.这对于分配是必不可少的
I have been trying to extract the extension of a given file for example "myfile.wld", so that later I can make a check to see if the right type of file has been entered at the terminal before I work on the contents of the file. This is necessary for an assignment
我已经使用函数"strtok"通过定界符"将输入分成多个部分.
I have used the function "strtok" to separate the input into sections by a delimiter "."
dot=strtok(argv[1], ".");
filename=dot;
文件名现在是输入"myfile"的第一部分,我的问题是如何到达."之后的第二部分,请告知,请耐心等待,以使其尽可能简单,以便我可以利用您的回复
filename is now the first part of the input "myfile", my question is how can I get to the second part after the ".", please advise, and please be patient enough to make it as simple as possible so that I can make use of your replies
谢谢
推荐答案
char *extension;
extension=strtok(NULL, ".");
在您上面的代码之后.
首先,像这样的strtok(str, ".")
一样,用指向str
的指针调用strtok()
.
然后继续调用strtok(NULL, ".")
作为下一个令牌.
如果返回的值为null(\0
),则它是字符串的结尾.
First, call strtok()
with pointer to str
like this strtok(str, ".")
.
Then keep calling strtok(NULL, ".")
for next token.
When the returned value is null (\0
) then it is end of string.
这篇关于提取文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!