As a fledgling programmer, I'm taking an intro programming class to learn C, and in my lab assignments I'm expected to have an output file for any output to the screen in my program, which means using fprintf() after any time I use printf(). However, I quickly found that (even with copy-pasting) typing fprintf() a bunch of times is pretty annoying, and it would be nice if I only had to call 1 function to output to both my program and to a designated output file.
So, I was wondering how I might create my own function to combine printf() and fprintf(), which after consulting my friend might be a bit more complex than I thought. Here's an example of how it would look in theory:
#include <stdio.h>
int main(void)
{
FILE *fpOpen;
fpOpen = fopen("output.txt", "w");
int a = 1488;
printToFileAndScreen(fpOpen, "a bunch of text goes here and then a number %d", a);
}
where printToFileAndScreen would be defined somewhere else in the program and printToFileAndScreen would take arguments for an output file as well as strings and format specifiers--basically just adding the functionality of fprintf() to printf(). I'm not aware if this function already exists in C, so if it does exist then I could simply use that function instead of making my own, and also please try to keep any answers pretty simple since I'm still very much learning the basics.
As an aside, I know that there is a way of using the command prompt in Windows to set up input and output files without needing to type fprintf a bunch of times, but from my limited understanding I believe that would not be a suitable solution when turning in my assignments (because my instructor wants only the source file and that's it) while I know that creating my own valid function and calling that would definitely work.