Printing on screen and a text file


I need to dump the certain things into a text file and same has needs to be displayed on screen. (I'm telling about a C program utiltiy)
The menu option looks like following,

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", 
fprintf(file_p, "Starting serial number       [%ld]\n", 
printf (        "Current Serial number         [%d]\n", 
fprintf(file_p, "Current Serial number         [%d]\n", 

Is there an easiest way to implement this to cut down the number of lines of code?

Best Solution

Edit: the C++ tag seems misleading, can someone remove it please? thanks :)

I use variadic macros to customize printf and friends.

I would write something like this:

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

(the name comes from the tee(1) utility)