0

I have a C program which takes an input from the command line, processes the input, generates a file and terminates.

For example myprogram.c lloks like:

int main(int argc, char *argv[])
{

//do something with the input
// and generate a file

exit(0);
}

I currently give an input as: echo -n abcd | myprogram.c

What I want the program to do is instead of terminating after one input, it should keep on executing and wait for the next console input to generate the next file and so on.

Can someone guide me?

hw-135
  • 162
  • 1
  • 15
  • 1
    If you want something to happen repeatedly, use a loop. If you never want it to stop, use an infinite loop, ie, one with a condition which is always true. And you don't need to put `exit(0)` at the end of main, it's entirely redundant. – Useless Oct 22 '19 at 10:58
  • 3
    Possible duplicate of [How do you read scanf until EOF in C?](https://stackoverflow.com/questions/3764014/how-do-you-read-scanf-until-eof-in-c) – Federico klez Culloca Oct 22 '19 at 10:58
  • @Useless, so basically wrapping the functionality within a `while(1)`? And how could I check whether the program is terminating or not and how can I give consecutive inputs to test this? As mentioned, currently my input is of the form `echo -n abcd | myprogram.c` – hw-135 Oct 22 '19 at 11:04
  • @rb-93 What would be the end condition? – Jabberwocky Oct 22 '19 at 11:24

2 Answers2

1

You can use a while loop:

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

bool function(char *input)
{
    // do something with the input
    // and generate a file

    // example:
    if (strcmp(input, "exit") == 0)
        return true;

    printf("I did something with '%s'\n", input);
    return false;
}

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        printf("Usage: program_name <argument>.");
        return 1;
    }

    function(argv[1]);

    bool exit = false;
    while (!exit)
    {
        char input[32];
        scanf("%s", &input);
        exit = function(input);
    }
    return 0;
}
Sasino
  • 134
  • 2
  • 11
0

What you are trying to do is get stdin back to your control (terminal input) after a redirection. According to the C-FAQ you can't do it in a portable way:

There is no portable way of doing this. Under Unix, you can open the special file /dev/tty. Under MS-DOS, you can try opening the ``file'' CON, or use routines or BIOS calls such as getch which may go to the keyboard whether or not input is redirected.

But, if portability is not an issue, we can try a little work around (not optimal):

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    char buf[1001], fname[10];
    FILE *fp;
    int i = 0;

    while(1) {
        if(fgets(buf, 1000, stdin)) {
            sprintf(fname, "mf%d", i++);

            if(!(fp = fopen(fname, "w"))) {
                perror("fopen");
                continue;
            }

            fprintf(fp, buf);
            fclose(fp);
        }

        if(!(stdin = fopen("/dev/tty", "r"))) {
            perror("stdin");
            exit(0);
        }
    }


    return 0;
}

The thing is, when you run the program, go to another terminal and get it's PID (ps aux | grep myprog). Say the PID is 12345 Then you do ls -la /proc/12345/fd and you'll see that the descriptor 0 is a pipe (done by the shell). So you need another stdin... and in Linux you can use fopen("/dev/tty", "r"), but again, not portable.

Enzo Ferber
  • 3,029
  • 1
  • 14
  • 24