1

I have the following code and if I try to redirect it to a file, no prompts are output. It redirects both the prompt and the resultant output to my file.

Is there a way to have it output prompts to stdout and then the output to the file?

/*prints chars in rows and columns*/
#include <stdio.h>

void display(char cr, int lines, int width); /*prototypes are fun*/

int main(void) {
    int ch; /*char to be printed*/
    int rows, cols; /*number of rows and columns*/
    printf("Enter a character and two integers.\n");

    while((ch = getchar()) != '\n') {
        if(scanf("%d %d", &rows, &cols) != 2) {
            break;
        }
        display(ch, rows, cols);
        while(getchar() != '\n') {
            continue;
        }
        printf("Enter another character and two integers. Newline quits.\n");
    }

    return 0;
}

void display(char cr, int lines, int width) {
    int row, col;
    for(row = 1; row <= lines; row++) {
        for(col = 1; col <= width; col++) {
            putchar(cr);
        }
        putchar('\n');
    }
}
4
  • 1
    When you redirect, the file is stdout. So anything you print to stdout will end up in the file. If you want to print to the tty, try printing to stderr (and don't redirect stderr). Commented Jul 9, 2014 at 1:29
  • What's the OS? You might find it easier to do this by piping to the tee command. Commented Jul 9, 2014 at 1:32
  • How do I use tee? program | tee > outfile does not work nor does program | tee outfile Commented Jul 9, 2014 at 1:41
  • Using tee won't solve your problem, because it won't send part of the output to the file and part to stdout. Use William's idea of printing the prompts to stderr, or my answer below printing the other output to a file. Commented Jul 9, 2014 at 2:03

2 Answers 2

1

You may want to make the filename an (optional) command-line argument.

To do this, you would change the beginning of your main like:

int main(int argc, char** argv) {
  FILE* outfile;
  if (argc == 2) outfile = fopen(argv[1], "w");
  else outfile = stdout;

(To be safe, you should probably check that outfile is not null in case there is an error opening the file. You might also want to check for improper number of command-line arguments, make a help message, and so on, depending on who this program is for.)

Then you have to pass this FILE* to the display function, which requires changing that function signature like

void display(char cr, int lines, int width, FILE* outfile) {

And finally, change your putchar calls to putc so you can send them to the file, like

putc('\n', outfile);

After all this, running your program like

./myprog

will print all prompts as well as the output to stdout, whereas running it with a filename like

./myprog file.txt

will still print the prompts to stdout, but the other output will go to file.txt.

Sign up to request clarification or add additional context in comments.

3 Comments

This is interesting as I have barely touched on programming FILE I/O in C. What is the FILE *outfile part? I have seen it before, but is FILE a special data type or what?
FILE is defined in stdio.h and it holds information on reading or writing to any file-like thing (could be a device, a terminal, or an actual file). You might want to read a tutorial on C I/O like this one
Thanks for the answer and the link to the tutorial
0

While dealing with redirect output to a file you may use freopen().

Though it's not a good solution, but it is a good example how to use freopen(). Assuming you are trying to redirect your stdout to a file 'output.txt' then you can write -

freopen("output.txt", "a+", stdout);  

Here "a+" for append mode. If the file exists then the file open in append mode. Otherwise a new file is created.

After reopening the stdout with freopen() all output statement (printf, putchar) are redirected to the 'output.txt'. So when you try to redirect the 'printf("Enter another character and two integers. Newline quits.\n");'statement again in terminal/command prompt then you have to reassign stdout again using the following code-

freopen("/dev/tty", "w", stdout); /*for gcc, ubuntu*/  

or

freopen("CON", "w", stdout); /*Mingw C++; Windows*/  

Here is the full code -

/*prints chars in rows and columns*/
#include <stdio.h>

void display(char cr, int lines, int width); /*prototypes are fun*/

int main(void) {
    int ch; /*char to be printed*/
    int rows, cols; /*number of rows and columns*/

    printf("Enter a character and two integersk.\n");

    while((ch = getchar()) != '\n') {
        if(scanf("%d %d", &rows, &cols) != 2) {
            break;
        }

        freopen("output.txt", "a+", stdout);
        display(ch, rows, cols);
        freopen("/dev/tty", "w", stdout); /*for gcc, ubuntu*/
        //freopen("CON", "w", stdout); /*Mingw C++; Windows*/

        printf("Enter another character and two integers. Newline quits.\n");
        while(getchar() != '\n') {
            continue;
        }
        fclose(stdout);
    }

    return 0;
}

void display(char cr, int lines, int width) {
    int row, col;

    for(row = 1; row <= lines; row++) {
        for(col = 1; col <= width; col++) {
            putchar(cr);
        }
        putchar('\n');
    }
    return;
}

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.