从文件或文件描述符中读取

时间:2015-05-27 09:41:46

标签: c file-io file-descriptor process-substitution

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

int main(int argc, char** argv) {
    FILE* input_file = fopen(argv[1], "rb");
    printf("%ld\n", ftell(input_file));
    fseek(input_file, 0, SEEK_END);
    printf("%ld\n", ftell(input_file));
    size_t length  = ftell(input_file);
    char* src   = malloc(length);
    assert(src != NULL);
    fseek(input_file, 0, SEEK_SET);
    fread(src, 1, length, input_file);
    fclose(input_file);

    // Do stuff with src
    free(src);
}

输出:

$ ./a.out somefile.txt 
0
4
$ ./a.out <(cat somefile.txt)
-1
-1
a.out: a.c:12: main: Assertion `src != ((void *)0)' failed.
Aborted

即使文件是通过bash进程替换获得的文件描述符,有没有办法让它工作?

0 个答案:

没有答案