0

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:

  1. Only a source file to read from is provided.

    Desired Behavior: Display the result of replacing the text to the standard output (terminal)

  2. 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.

  3. 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.

the busybee
  • 10,755
  • 3
  • 13
  • 30
croblin
  • 61
  • 4
  • Create two `FILE *` variables, which you can name `In` and `Out`. If the user provides an input file name, open it with `fopen` and set `In` to the return value from `fopen`. If they do not, set `In` equal to `stdin`. If the user provides an output file name, open it with `fopen` and set `Out` to the return value from `fopen`. If they do not, set `Out` equal to `stdout`. Read from `In` and write to `Out`. – Eric Postpischil Feb 05 '22 at 00:47
  • You can do it with `freopen()`. The third argument would be either `stdin` or `stdout`, depending on whether you're providing theinput file or the output file. – Barmar Feb 05 '22 at 00:51
  • What would I pass as parameters to ```freeopen()``` if no file is provided? For example: no source file is provided: ```freeopen("createdSrc.txt", "r", stdin)```? – croblin Feb 05 '22 at 00:57

2 Answers2

0

The third argument is which of the standard file streams you want to replace with a file.

So the logic will be something like:

const char *input_file = NULL;
const char *output_file = NULL;

// parse arguments, setting the above variables to the appropriate arguments if supplied

if (input_file) {
    freopen(input_file, "r", stdin);
}

if (output_file) {
    freopen(output_file, "w", stdout);
}

Then the rest of the program uses stdin and stdout normally.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Okay, so in the case of there being an output file but no input file, we use stdin to capture the input then write it to a destination file. Would that look like this?```freopen(input_file, "r", stdin); fopen(output_file, "w"); ``` ? – croblin Feb 05 '22 at 01:10
  • No. When there's no input file, the `if` statement will just skip over the `freopen()`. So stdin will stay connected to its default value. – Barmar Feb 05 '22 at 01:12
0

The traditional (unixoid) way is to use only stdin and stdout.

If you want to provide an input file, use input redirection:

your_program < input_file

If you want to provide an output file, use output redirection:

your_program > output_file

If you want to provide an input file and an output file, use both ways of redirection:

your_program < input_file > output_file

And you could even add your program to a chain of commands, using pipes:

run_before | your_program | run_after

This gives you a maximum of flexibility.

It is still possible to provide options to your program.

the busybee
  • 10,755
  • 3
  • 13
  • 30