系统(文件)命令的输出储存在C字符串 [英] Store output of system(file) command as a string in C
本文介绍了系统(文件)命令的输出储存在C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要获取文件,我们可以执行命令的类型
To get the type of file we can execute the command
system("file --mime-type -b filename");
使用命令输出显示到terminal.But不能存储文件类型
The output displays in to terminal.But could not store the file type using the command
char file_type[40] = system("file --mime-type -b filename");
那么,如何使用系统(文件)函数来存储文件类型为字符串。
So how to store file type as a string using system(file) function.
推荐答案
您可以使用的popen
是这样的:
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char *argv[] )
{
FILE *fp;
char file_type[40];
fp = popen("file --mime-type -b filename", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit -1;
}
while (fgets(file_type, sizeof(file_type), fp) != NULL) {
printf("%s", file_type);
}
pclose(fp);
return 0;
}
这篇关于系统(文件)命令的输出储存在C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文