Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ftell总是返回-1_C++_Ubuntu - Fatal编程技术网

C++ ftell总是返回-1

C++ ftell总是返回-1,c++,ubuntu,C++,Ubuntu,我正在尝试编写一个函数,该函数将按字符串中的程序名终止实例 unsigned int kill_all_program_instances(const char * program_name){ int res; char buf[1024]; string first; string second; int lSize, pid , pos; string command="pidof "; FILE *fd; memset(bu

我正在尝试编写一个函数,该函数将按字符串中的程序名终止实例

unsigned int kill_all_program_instances(const char * program_name){
    int res;
    char buf[1024];
    string first;
    string second;
    int lSize, pid , pos;
    string command="pidof ";
    FILE *fd;
    memset(buf,'\0',sizeof(buf));
    fd=popen((command+program_name).c_str(),"r");
    unsigned int mypid = getpid();
    if(fd != NULL){ 
    fseek (fd , 0 , SEEK_END);
    lSize = ftell(fd);
    rewind (fd);
    if (lSize <= 0)
    {
        printf("lsize is %d\n",lSize);
        pclose(fd);
        return 0;
    }
            .....
}
得到

26487 19353 16993 11504 10960 10880 10868 10829 10825 10805 8607 8263 8154 8089 7764 3965 3950
但是当我跑的时候

kill_all_program_instances('chromium-browse')
对于lSize,我仍然得到-1分

知道我的代码有什么问题吗


谢谢

除了关于检测
popen
错误的评论/回答(这似乎不正确)之外,我相信实际的问题是您试图在管道上使用fseek/ftell,这不是一个有效的操作。管道不是传统意义上的“文件”,它是一个数据流,其中每个数据项只能读取一次


只要使用
fscanf()
或类似的方法阅读,直到它给出一个EOF

除了关于检测
popen
错误的评论/回答(这似乎不正确)之外,我确信实际的问题是您试图在管道上使用fseek/ftell,这不是一个有效的操作。管道不是传统意义上的“文件”,它是一个数据流,其中每个数据项只能读取一次

只要使用
fscanf()
或类似的方法阅读,直到它给出一个EOF

popen()使用管道与生成的进程通信。管道是不可查找的

您不能对popen返回的文件*使用fseek/ftell,它们总是会失败。

popen()使用管道与生成的进程通信。管道是不可查找的


你不能在popen返回的文件*上使用fseek/ftell,它们总是会失败。

你试过查看
errno
strerror()
吗?你能检查一下
errno
等是否有错误吗?也可以查看fseek的返回值。你试过查看
errno
strerror()吗
?您能否检查
errno
等是否存在错误?请参见检查fseek的返回值。
kill_all_program_instances('chromium-browse')