gpt4 book ai didi

c - 如何获取内存中缓冲区的文件描述符?

转载 作者:太空狗 更新时间:2023-10-29 16:36:01 26 4
gpt4 key购买 nike

如果我有一个包含文件数据的缓冲区,我如何从中获取文件描述符?这是一个源自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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com