我有以下代码示例(在 Windows 中):
int fd = _dup(fileno(stdout));
freopen("tmp","w",stdout);
printf("1111");
close(stdout);
char buf[100];
FILE *fp;
fp = fopen("tmp","r");//in this line fd turns to be 0
if(NULL == fp) return -1;
if(fgets(buf,100 , fp) != NULL );
else return -1
fclose(fp);
我需要 fd 的值以供进一步使用。如何从文件中读取而不丢失 fd
值?
最佳答案
我敢打赌,要么 buf 被声明为小于 100,要么 fgets 调用实际上使用的数字大于 100。
关于c - fopen + Stream _dup 中的流描述符丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7724631/