开发者

how to read stderr into char[] in c

开发者 https://www.devze.com 2023-01-28 03:26 出处:网络
I have the code to check STDERR with \'DRM protected stream detected\': const static char* DRM_TOKEN = \"DRM protected stream detected\";

I have the code to check STDERR with 'DRM protected stream detected':

const static char* DRM_TOKEN = "DRM protected stream detected";

const char* source = argv[1];
char tempfile[80];
memset(tempfile, 0, 80);
snprintf(tempfile, 80, "stderr_%lld.log", av_gettime());
freopen(tempfile, "w", stderr);

fflush(stderr);
FILE *fp = fopen(tempfile, "r");
if(fp)
{
    char STDERR[256];
    while(!feof(fp))
    {
    memset(STDERR, 0, sizeof(char) * 256);
    fgets(STDERR, 256, fp);
    if(strstr(STDERR, DRM_TOKEN) != NULL)
    {
        drm = 1;
        break ;
    }
    }
    fclose(fp);
}

It works, but I want to know any directly开发者_如何学JAVA way to read STDERR into char[]. PS. my code will run in linux or macos.


stderr was reopened in "w" mode, which is write-only. Judging from your av_gettime function, it looks like you're using libavcodec/libavutil, so instead of writing hideous hacks like this to get the error messages, you should read up on the av_log system and register your own logging function to override the default behavior of writing diagnostic messages to stderr.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号