This shell cmd syntax
./program < input_file > output_file
doesn't have to do anything with the parameters passed to main
int main(int argc, char** argv) {
You can just refer to std::cin and std::cout for the mentioned input and output files.
It's a shell feature and called standard input/output stream redirection. If you want to pass extra parameters to your program via argv you'll usually use the following syntax
./program -x --opt1 < input_file > output_file
As you state
"I'm only allowed to work with the stdio.h so please keep it basic."
In this case you can use the predefined stdin/stdout macros.
FILE* filein = stdin;
FILE* fileout = stdout;
This is what you should use as default. If you want to have the additional feature, that the user specifies particular input/output file names as program arguments, you should also check the command line parameters passed to your main:
int main(int argc, char* argv[]) {
FILE* filein = stdin;
FILE* fileout = stdout;
if(argc > 1) {
filein = fopen(argv[1], "r");
}
if(argc > 2) {
fileout = fopen(argv[2], "w");
}
}
By default your program reads from standard input and writes to standard output, if done as above. So just calling the program without any parameters, leaves a prompt to the user to input something.
This is the preferred, and most flexible style for implementing console programs, that should process streamed input and transform (process) to any output.
fopen. If you usecinyou will read the file that was piped to your program. Likewise, the output fromcoutwill be piped to the output file presented to your program.