Freopen (stdin stdout) details + instances

Source: Internet
Author: User

 

Find a large circle... now you can see something you can understand.

Think slowly when you cannot figure it out! Something you can understand. Read it carefully...

This is a bit difficult to find on the Internet. Code:

# Include <stdio. h> <br/> # include <unistd. h> <br/> int <br/> main () <br/> {<br/> file * FP; <br/> char Buf [1024]; <br/> char tty [32]; <br/> ttyname_r (fileno (stdin), tty, sizeof (TTY); <br/> fp = freopen (". /freopen. C "," r ", stdin); <br/> fgets (BUF, sizeof (BUF), stdin); <br/> printf (" % s ", Buf ); <br/> fp = freopen (TTY, "r", stdin); <br/> fgets (BUF, sizeof (BUF), stdin ); <br/> printf ("% s", Buf); <br/> return 0; <br/>} 


 

Change "/dev/console" to "/dev/tty" to "F7 A8 u * Z1 e" K &{-/
7 U9 h) N * D "K5 V; L
/Dev/Console indicates the system console. Each Linux system has a dedicated terminal or display screen to receive console messages. It is usually an active virtual console. In X Windows, it is a special Console window on the screen.

The special file/dev/tty is an alias (Logical Device) of the process control terminal (keyboard and display, or keyboard and window)-if this program has a control terminal. /Dev/tty allows the program to output information directly to the user, regardless of the type of Pseudo Terminal used by the user. This feature is useful when the standard output is redirected.

There is only one/dev/console device, but there are countless physical devices available through/dev/tty.

 

How to restore the standard handle after freopen by skywind

You can usually use freopen to redirect input/output to a file. For example
[Cpp1_freopen(”in.txt "," R ", stdin );
Freopen(export out.txt "," W ", stdin );
[/CPP]

However, there is no completely compatible solution that can restore the standard handle later. There is no way in the C standard library.
The easy way to think of is to re-open the standard console device file, but unfortunately the name of this device file is related to the operating system.

  • In DOS/win, the name is con, so you can use

    [CPP] freopen ("con", "R", stdin) [/CPP]

  • In Linux, the console device is/dev/console

    [CPP] freopen ("/dev/console", "R", stdin) [/CPP]

In addition, in Unix-like systems, you can use the DUP system call to pre-copy an original stdin handle.

 

Using the C ++ stream library, you can use another method to achieve the freopen effect, and it is easy to restore. It is the rdbuf that uses the stream.For example:

Ofstream outf({out.txt "); <br/> streambuf * strm_buf = cout. rdbuf (); <br/> cout. rdbuf (outf. rdbuf (); <br/> cout <"write something to file"; <br/> cout. rdbuf (strm_buf); // recover <br/> cout <"display something on screen ";

========================================================== ==============================
Function Name: freopen
Function: replace a stream
Usage: file * freopen (char * filename, char * type, file * stream );
Program example:
# Include <stdio. h>
Int main (void)
{
/* Redirect standard output to a file */
If (freopen ("output. fil", "W", stdout)
= NULL)
Fprintf (stderr, "error redirectingstdout/N ");
/* This output will go to a file */
Printf ("This will go into a file .");
/* Close the standard output stream */
Fclose (stdout );
Return 0;
}

========================================================== ==============================
If you don't understand the above, you can look down. It doesn't matter. Practice + theory will be explained here slowly.
The following highlights:

Let's talk about it here. Otherwise it's hard to understand. I didn't even think .. it's always confusing... stdin stdout stderr.
Now I understand.

Remember: Currently, the main cache features are: stdin and stdout are row-based caches, while stderr is non-cached.
This article describes how to redirect stdout to a program with a file from a certain C, and then restore a higher position of the same original stdout program. The C function is usually used to redirect stdout or stdin to freopen (). To redirect stdout to file. txt, use the following call:

Freopen ("file.txt", "W", stdout); // write the content to this file "file.txt"

This statement usually redirects all subsequent output to file. txt stdout.

To return the stdout that displays the default stdout, use the following call:

Freopen ("con", "W", stdout); // output to console "con"

In both cases, check freopen () to ensure that the actual returned value of the redirection occurs.

The following is a short program that demonstrates stdout redirection:

Run code

// Compile options needed: none
#include <stdio.h>
#include <stdlib.h>void main(void)
{

File * stream;

// Write the content to file.txt,"W" is write ("R" is read)
If (Stream = freopen ("file.txt", "W", stdout) = NULL)
Exit (-1 );
Printf ("this is stdout output/N ");
Stream = freopen ("con", "W", stdout); stdout is a redirection to the console at the end of the program
Printf ("And now back to the console once again/N ");
}

"Con" refers to the DOS window on the console.
========================================================== =
Run the Code:
# Include <stdio. h>
# Include <stdlib. h>

Void main (void)
{
File * stream;
Char s [102400] = "";
If (Stream = freopen ("file.txt", "r", stdin) = NULL) // read data from a file (put in stdin, in fact stdin also has its own buffer. to BUF)
Exit (-1 );

Fread (s, 1, 1024, stdin); // read data from the standard input, because stdin also has its own buffer.

Printf ("% s/n", S); // print the read data here.

}

 

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.