【发布时间】:2011-04-09 02:09:25
【问题描述】:
我正在尝试使用 c 代码找出文件类型,这是代码
char *get_file_type(char *path, char *filename)
{
FILE *fp;
char command[100];
char file_details[100];
char *filetype;
sprintf(command, "file -i %s%s", path, filename);
fp = popen(command, "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
while (fgets(file_details, sizeof(file_details)-1, fp) != NULL) {
filetype = (strtok(strstr(file_details, " "), ";"));
}
pclose(fp);
return filetype;
}
这里不用声明command[],我可以使用*command 吗?我尝试使用它,但它引发了异常。我们不需要释放声明为 command[] 的变量吗?如果是怎么办?
【问题讨论】: