简体   繁体   English

将终端输出到C程序中的文件

[英]Piping terminal output to file from within C program

I would like everything that gets output to stdout to also be saved in a file in my C code. 我希望将输出到stdout的所有内容也保存在C代码的文件中。 I know that I can do this by calling the process on the command line and piping it to a file: 我知道我可以通过在命令行上调用该过程并将其通过管道传输到文件来做到这一点:

 myprogram.exe 1>logfile.txt

for example. 例如。 But I am wondering if there is a way to do this from within the C code itself. 但是我想知道是否有一种方法可以从C代码本身内部做到这一点。 Is there a function in the printf() family that can output to both the terminal and a specified file with the same arguments? printf()系列中是否有一个函数可以输出到终端和具有相同参数的指定文件?

If not, what would be the syntax required to write my own printf()-style function which calls both printf() and fprintf() within it, using the same argument style as printf()? 如果不是,编写自己的printf()样式的函数(使用与printf()相同的参数样式)在其中调用printf()和fprintf()的语法将是什么?

You can use the fprintf() function, which is quite similar to printf() in the way it works. 您可以使用fprintf()函数,该函数在工作方式上与printf()非常相似。

Here is an example: 这是一个例子:

FILE *fp;
int var = 5;
fp = fopen("file_name.txt", "w");// "w" means that we are going to write on this file
fprintf(fp, "Writting to the file. This is an int variable: %d", var);

The output on your file would be this: 您文件的输出将是这样的:

This is being written in the file. This is an int variable: 5

NB: Opening the file using w as parameter will destroy the file's content every time you open it. 注意:每次使用w作为参数打开文件都会破坏文件的内容。

For writing to a file you have to use file operation commands, it is not possible to write to a file using printf (it prints only to the stdout). 要写入文件,必须使用文件操作命令,无法使用printf写入文件(它仅打印到stdout)。 You can use: 您可以使用:

sprintf(buf,"%d",var);  //for storing in the buffer
printf(buf);       //output to stdout
fputs(buf, fp);   //output to file

Going off the suggestion to use variadic functions: 放弃使用可变参数函数的建议:

#include <stdio.h>
#include <stdarg.h>

/*
 * Not all compilers provide va_copy(), but __va_copy() is a
 * relatively common pre-C99 extension.
 */
#ifndef va_copy
#ifdef __va_copy
#define va_copy(dst, src) __va_copy((dst), (src))
#endif
#endif

#ifdef va_copy
#define HAVE_VACOPY 1
#endif

int
ftee(FILE *outfile, const char *format, ...)
{
    int result;
    va_list ap;
#if HAVE_VACOPY
    va_list ap_copy;
#endif

    va_start(ap, format);

#if HAVE_VACOPY
    va_copy(ap_copy, ap);
    result = vfprintf(outfile, format, ap_copy);
    va_end(ap_copy);
    if (result >= 0)
        result = vprintf(format, ap);
#else
    result = vfprintf(outfile, format, ap);
    if (result >= 0) {
        va_end(ap);
        va_start(ap, outfile);
        result = vprintf(format, ap);
    }
#endif
    va_end(ap);
    return result;
}

That can be used like the standard fprintf function in that you specify an output file, except it will also write the normal output to stdout . 可以像标准fprintf函数一样使用它,因为您可以指定输出文件,除了它还会将常规输出写入stdout I attempted to support relatively recent compilers that still didn't have a va_copy() macro (defined in C99) such as that which comes with Visual Studio 2012 (VS2013 finally has one). 我试图支持相对较新的编译器,这些编译器仍然没有va_copy()宏(在C99中定义),例如Visual Studio 2012附带的宏(VS2013最终有了一个)。 Some C runtimes also conditionally define va_copy() such that compiling with strict C89/C90 mode enabled will make it undefined while __va_copy() may remain defined. 一些C运行时还会有条件地定义va_copy() ,以便在启用严格的C89 / C90模式的情况下进行编译将使其未定义,而__va_copy()可能仍保持定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM