I am trying to write a program that ideally accepts arguments that specify both a source file (to read from) and a destination file (to write to).
The program replaces specified characters in source file, with other specified characters, then writes the output to the destination file.
There are 3 special cases I want to handle, though:
Only a source file to read from is provided.
Desired Behavior: Display the result of replacing the text to the standard output (terminal)
Only a destination file to write to is provided.
Desired Behavior: Read from standard input, replacing desired letters and writing result to destination file name provided.
Neither the source file nor the destination file are provided.
Desired Behavior: Read from the standard in, replace characters, write to standard out.
I believe the way to do this is using freopen()
, but I cannot wrap my head around the logic of when the file gets opened or how freopen()
3rd argument (stream) works.
What is the traditional way of handling this problem? Is freopen()
even the function I am looking for to do this? Or is there some great lesser known function made for situations like these?
Some pseudo code would be really appreciated.