c - 在屏幕和文本文件上打印

标签 c text tee

我需要将某些内容转储到文本文件中,并且需要在屏幕上显示相同的内容。 (我说的是 C 程序实用程序) 菜单选项如下所示,

1. display AA parameters
2. display BB parameters
3. display CC parameters
4. dump all
5. Exit
Select option >

If they select 1/2/3, it just needs to displayed on screen only or if they select option #4,it need to display all the parameters one by one and same needs to dumped in a .txt file.

I know, we can use the printf and fprintf functions to display on screen and write it to text file respectively. The thing is that I've display more that 20 parameters and each have at least 20 sub-parameters.

I'm currently implemented as below,

printf (        "Starting serial number       [%ld]\n", 
        serial_info_p->start_int_idx);
fprintf(file_p, "Starting serial number       [%ld]\n", 
        serial_info_p->start_int_idx)
printf (        "Current Serial number         [%d]\n", 
        serial_info_p->current_int_idx);
fprintf(file_p, "Current Serial number         [%d]\n", 
        serial_info_p->current_int_idx);

有没有最简单的方法来实现这个以减少代码行数?

最佳答案

编辑:C++ 标签似乎具有误导性,有人可以删除它吗? 谢谢 :)

我使用可变参数宏来自定义 printf 和 friend 。

我会这样写:

#define     tee(fp,fmt, ...)                             \
        {                                                \
                printf (fmt, __VA_ARGS__);               \
                fprintf (fp, fmt, __VA_ARGS__);          \
        }

(名称来自 tee(1) 实用程序)

关于c - 在屏幕和文本文件上打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/444111/

相关文章:

regex - 使用下一个匹配作为分隔符来匹配字符串

bash - 如何有效地将一个文件流式传输到多个管道

可以安全地将 objc_msgsend 转换为可变长度参数函数

c++ - 0和128的区别

java - Android 手机上的文本文件

text - 如何更改apache poi word文档中的文本方向(不是段落对齐)?(XWPF)

linux - 在 bashrc 中使用 linux "tee"命令

python - crontab tee 命令不会将 stdout 写入 txt 文件,而是清除它 [python 脚本]

c++ - 为什么 int* ptr_arr_int = {1,2};在 C/C++ 中不起作用?

c - 可靠的UDP实现设计问题