首页 > 代码库 > freopen内部实现

freopen内部实现

 1 #include <unistd.h>
 2 #include <stdio.h>
 3 
 4 FILE* Freopen(const char *fileName, const char *type, FILE *stream){
 5   FILE *fileFp = fopen(fileName, type);
 6   int fd1 = fileno(fileFp);
 7   int fd2 = fileno(stream);
 8   if(dup2(fd1, fd2) < 0) return NULL;
 9   else return stream;
10 }