Redirection and restoration of printf

Source: Internet
Author: User

Print the row 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 an incorrect 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 is printed to a file! \ N ");
Fclose (stdout );
Stdout = copy;
Printf ("where is this line? \ N ");
Return 0;
}
Similar to copy = stdout; direct value assignment is not feasible. DUP and dup2 should be used. The correct one 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 is printed to screen! Bak_out is % d \ n ", bak_out );
Copy = fopen ("stdio_test.txt", "W ");
Dup2 (fileno (copy), 1 );
Printf ("this is printed to a file! \ N ");
Fclose (copy );
Dup2 (bak_out, 1 );
Printf ("where is this line? \ N ");
Return 0;
}

At the beginning, freopen was used. The Code is as follows:
Copy = freopen ("stdio_test.txt", "W", stdout );
Printf ("this is printed to a file! \ N ");
Fclose (copy );
Dup2 (bak_out, 1 );
Printf ("where is this line? \ N ");
In this way, although redirection can be performed, it cannot be restored, and dup2 cannot be used. There is only one feasible method, but it cannot be transplanted because the terminal names of Windows and Linux are different.
(Applicable to Windows)
File * fp = freopen ("A. Out", "W", stdout );
// Printf
Fflush (FP); // clears the output buffer.
Freopen ("con", "W", stdout); // export data to the console

(In Linux)
File * fp = freopen ("A. Out", "W", stdout );
// Printf
Fflush (FP); // clears the output buffer.
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 shoshould 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.