Control comments and answers output by cout and print in C ++

Source: Internet
Author: User

The lhslktg friend of the forum posted a post about whether to use the fastest method by calling a lot of cout output in his program, this allows the program output to be directed to a file. I understand the so-called fast method, that is, try not to change the original program, or at least not to change the internal program to achieve this function.

A friend gave me the best way to relocate the command output. If the application name is testcmd, run the following command:
Testcmd> test. Log

Write the cout output in the command to the file.
Some friends have added a standard output re-open statement at the beginning of the program:

Freopen ("test. log", "W", stdout)

In this way, all the output under this statement will be written to the test. log file.

But then the friend had a new requirement to keep the output of the original screen and write it into the file.
I have provided the following solutions.

Testcmd | tee test. Log

Here, tee is a command in a UNIX system. It acts as a t-connector of the pipeline just like its name. The output is divided into two streams, one to test. log, and the other to the screen. Next, I will give

The simple source code implementation of TEE is provided to be used on systems without the tee command. The source code is as follows:

# Include <stdio. h>

Int
Main (INT argc, char ** argv)
{
File * FP;
Int C;

If (argc> 1 ){
Fp = fopen (argv [1], "W ");
If (FP = NULL ){
Fprintf (stderr, "% s: can not open <% S>/N", argv [0], argv [1]);
Return-1;
}
} Else
Fp = stdout;

While (C = fgetc (FP ))! = EOF)
Fputc (FP );

Return 0;
}

Unexpectedly, I did not know if the landlord did not carefully read my instructions and asked me how to use this code. I should understand the description. Compile the program into an executable file, name it as tee, and then use it as described above:

Testcmd | tee test. Log

The output of the testcmd program is both output to the file and output to the screen. I don't know if I understand it.

So far, there have been new demands. That is to say, in the original program, there are both cout output and printf output. Can I separately control the two outputs to the screen and different files. For example, you can output the couts to cout.txt and the printf to printf. Out. I added the following code at the beginning of the program:

1 # include <iostream>
2 # include <fstream>
3
4 class dostream: public STD: ostream {
5 public:
6 dostream (): OFS ("cout.txt "){}
7
8 template <typename _ T>
9 dostream & operator <(_ T & Data ){
10 STD: cout <data;
11 OFS <data;
12
13 return * this;
14}
15
16 private:
17 STD: ofstream OFS;
18 };
19
20 dostream dout;
21
22 # define cout dout

The landlord continues to ask:
How does one control the output of cout?
What is the effect of adding a program directly at the beginning?

Now, let me explain this program:

Here, we have designed the dostream class from row 3 and inherited the ostream class. That is to say, it inherits all the functions of ostream. Don't forget that cout is a subclass object of ostream! This class is very simple. In the structure, a cout.txt output file is opened. To control the output of cout, that is, to control the <operator of cout. Therefore, a <reload template function is defined from row 3. The output data type here becomes a template parameter because it needs to overload the output of various types. In the hosts file.

Row 3 defines an object dout of dostream.
Line 3 defines cout as dout, so that during preprocessing, all the calls to cout in the program will be replaced with dout, that is, the function of the newly defined dostream class is called. And this

The main kinetic energy is manifested in the <operator overload, which calls our implemented overload function, writes the output to the screen, and outputs it to the file.

If you still don't understand it, we will explain it from the actual example.
Suppose there is a sentence in the original program:

Cout <"Hello/N ";

When the above program is included at the beginning of the program, the following situation occurs:
During preprocessing, the macro is replaced according to the 22 rows of the above program, so that cout> "Hello/N" is replaced:
Dout <"Hello/N ";

During compilation, this statement uses the template function defined in the above 8th rows. The template parameter is string, that is, the following function will be called:
Dostream & operator <(string & data );
Here DATA = "Hello/N", that is, dostream & operator <("hello ");
To call 10 rows and 11 rows, output "hello" to the screen and file respectively.

In addition, the following describes how to control the printf output. Similarly, the following code is provided:

# Include <stdio. h>
# Include <stdarg. h>

File * OFP;

Void
Init_printf (void)
{
OFP = fopen ("printf.txt", "W ");
If (OFP = NULL ){
Fputs ("can not open <printf.txt>/N", stderr );
Exit (-1 );
}
}

Int
Printf (const char * format ,...)
{
Va_list AP;
Int RC;

Va_start (AP, format );
Vprintf (format, AP );
Rc = vfprintf (OFP, AP, format );
Va_end (AP );

Return RC;
}

This code can be edited as a source program file, for example, printf. C. At the beginning of the original program, add the following sentence:
Init_printf ()
Then, use the following command to compile the link:

CC-O testcmd. cpp printf. c

Testcmd. cpp is assumed to be the original program. In this way, the program will first call printf. the function in C, that is, the printf we wrote above, does not call the printf in the standard library, and implements the function that outputs both to the screen and to the file. The above program calls the C language standard library's methods and techniques for Variable Parameter functions. If you do not understand it, please refer to relevant books.

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.