我需要将某些内容转储到文本文件中,并且需要在屏幕上显示相同的内容。 (我说的是 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/