开发者

Stream descriptor loss in fopen + stream _dup

开发者 https://www.devze.com 2023-04-12 17:50 出处:网络
I have the following code example (in windows): int fd = _dup(fileno(stdout)); freopen(\"tmp\",\"w\",stdout);

I have the following code example (in 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);

I need the value of fd for the futher use.How can I开发者_C百科 read from file without losing the fd value?


I bet that either buf is declared smaller than 100 or the fgets call is in fact with a number larger than 100.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号