using 'file' in a cpp-program

hi all,
I want to use the file command like so:
 
int rc = system( "file -b anyfile.txt" );

How can I get this command's output (e.g. "ASCII English text")?

thanks for any advice
testalucida
Last edited on
found it out - it works like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void use_file( const string & filename ) {
    FILE *fpipe;
    string command( "file -b " + filename );
    char filetype[256];

    fpipe = (FILE*)popen( command.c_str(), "r" );
    if( !fpipe ) {
        perror("Problems with pipe");
        exit(1);
    }

    fgets( filetype, sizeof filetype, fpipe );
    printf("%s", filetype);

    pclose(fpipe);
}
Topic archived. No new replies allowed.