Categories:
.NET (357)
C (330)
C++ (183)
CSS (84)
DBA (2)
General (7)
HTML (4)
Java (574)
JavaScript (106)
JSP (66)
Oracle (114)
Perl (46)
Perl (1)
PHP (1)
PL/SQL (1)
RSS (51)
Software QA (13)
SQL Server (1)
Windows (1)
XHTML (173)
Other Resources:
How can I arrange to have output go two places at once, e.g. to the screen and to a file?
How can I arrange to have output go two places at once, e.g. to the screen and to a file?
✍: Guest
You can't do this directly, but you could write your own printf variant which printed everything twice. Here is a sample logprintf function which prints to both stdout and a preopened log file:
#include <stdio.h>
#include <stdarg.h>
extern FILE *logfp;
void logprintf(char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
vfprintf(stdout, fmt, argp);
va_end(argp);
va_start(argp, fmt);
vfprintf(logfp, fmt, argp)
;
va_end(argp);
}
Now, whenever you call logprintf (which you can call with format strings just like printf), it prints both to stdout and to logfp, which you have presumably opened to your desired log file. Another way to arrange this would be
void f2printf(FILE *fp1, FILE *fp2, char *fmt, ...)
{
va_list argp;
va_start(argp, fmt); vfprintf(fp1, fmt, argp); va_end(argp);
va_start(argp, fmt); vfprintf(fp2, fmt, argp); va_end(argp);
}
where f2printf is just like fprintf except that you give it two file pointers (e.g. stdout and logfp) and it prints to both of them.
Both of these techniques obviously require you to use explicit calls to logprintf or f2printf. There is no known way in Standard C to arrange implicitly (i.e. via some call analogous to freopen) that one stream, which you print to once with a normal call like fprintf, print to two places at once.
2015-09-29, 1033👍, 0💬
Popular Posts:
Which is the best place to store ConnectionString in Dot Net Projects? I am about to deploy my first...
What are some uses of Intranets & Extranets? An "intranet" is the generic term for a collect...
How To Get the Last ID Assigned by MySQL? - MySQL FAQs - Managing Tables and Running Queries with PH...
What is the benefit of using #define to declare a constant? Using the #define method of declaring a ...
.NET INTERVIEW QUESTIONS - Can we use events with threading ? Yes, you can use events with thread; t...