如果我有一个包含文件数据的缓冲区,我如何从中获取文件描述符? 这是一个源自how to untar file in memory的问题
最佳答案
我写了一个简单的示例如何将文件描述符制作到内存区域:
#include <unistd.h>
#include <stdio.h>
#include <string.h>
char buff[]="qwer\nasdf\n";
int main(){
int p[2]; pipe(p);
if( !fork() ){
for( int buffsize=strlen(buff), len=0; buffsize>len; )
len+=write( p[1], buff+len, buffsize-len );
return 0;
}
close(p[1]);
FILE *f = fdopen( p[0], "r" );
char buff[100];
while( fgets(buff,100,f) ){
printf("from child: '%s'\n", buff );
}
puts("");
}
关于c - 如何获取内存中缓冲区的文件描述符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1558772/