Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store output of system(file) command as a string in C

Tags:

c++

c

To get the type of file we can execute the command

system("file --mime-type -b filename");

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.

like image 737
user2547731 Avatar asked Dec 27 '22 00:12

user2547731


1 Answers

See the man page of system: It does not return the output of the command executed (but an errorcode or the return value of the command).

What you want is popen. It return a FILE* which you can use to read the output of the command (see popen man page for details).

like image 82
flolo Avatar answered Jan 07 '23 22:01

flolo