Redirection and recovery for printf

Source: Internet
Author: User
Tags assert
Print the line information to the Stdio window, and then a row to the file, followed by a row to the stdio screen.
Let's take a look at the wrong example.




#include <ansi_c.h>
int main (int argc, char *argv[])
{
FILE *copy;




printf ("This is printed to screen!\n");
copy = stdout;
stdout = fopen ("Stdio test.txt", "w");
printf ("This are printed to a file!\n");
Fclose (stdout);
stdout = copy;
printf ("Where is this line?\n");
return 0;
}
Similar to copy = stdout; Direct assignment is not feasible, should be used dup,dup2, the correct should be the following




#include <stdio.h>
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#define Fileno _fileno
#define DUP2 _dup2




int main (int argc, char *argv[])
{
FILE *copy;
int bak_out = DUP (1)//can use Fileno (stdout) to replace 1
printf ("This are printed to screen! Bak_out is%d\n ", bak_out);
copy = fopen ("Stdio_test.txt", "w");
Dup2 (Fileno (copy), 1);
printf ("This are printed to a file!\n");
fclose (copy);
Dup2 (bak_out,1);
printf ("Where is this line?\n");
return 0;
}


At first it was freopen, and the code was as follows
copy = Freopen ("Stdio_test.txt", "w", stdout);
printf ("This are printed to a file!\n");
fclose (copy);
Dup2 (bak_out,1);
printf ("Where is this line?\n");
This can be redirected, but not recoverable, with dup2 is not possible, there are only one feasible method, but not portable, because Windows and Linux terminal name is not the same
(Applicable under Windows)
FILE *fp=freopen ("A.out", "w", stdout);
Printf
Fflush (FP);//Empty the output buffer, you can not
Freopen ("CON", "w", stdout); Directed output to the console




(Used under Linux)
FILE *fp=freopen ("A.out", "w", stdout);
Printf
Fflush (FP);//Empty the output buffer, you can not
Freopen ("/dev/tty", "w", stdout);
Freopen ("/dev/tty", "R", stdin);






Two other examples
#include <stdio.h>

void F () {
printf ("StdOut in F ()");
}

Main () {
int FD;
fpos_t POS;

printf ("stdout,");

Redriect
Fflush (stdout);
Fgetpos (stdout, &pos);
FD = DUP (Fileno (stdout));
Freopen ("Stdout.out", "w", stdout);

f ();

Fflush (stdout);
Dup2 (FD, Fileno (stdout));
Close (FD);
Clearerr (stdout);
Fsetpos (stdout, &pos); /* for c9x * *

printf ("stdout again\n");
}








#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
/* GCC defined UNIX * *
#ifdef UNIX
#include <unistd.h>
#endif
#ifdef WIN32
#include <io.h>
#define PIPE (X) _pipe (x,4096,o_binary)
#define Fileno _fileno
#define DUP2 _dup2
#define READ _read




#endif
#include <assert.h>




int main ()
{
int fds[2];
int res;
Char buf[256];
int so;




Res=pipe (FDS);
ASSERT (res==0);




So=fileno (stdout);
Close stdout handle and make the writable part of FDS the new stdout.
Res=dup2 (FDS[1],SO);
ASSERT (Res!=-1);




printf ("Hi there\n");
Fflush (stdout);
Reading should happen in a different thread




Res=read (fds[0],buf,sizeof (BUF)-1);
ASSERT (res>=0 && res<sizeof (BUF));
buf[res]=0;
fprintf (stderr, "buf=>%s\n", buf);
return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.