-1

I did tried to use this and this but couldn't make it right (please notice the links before referring to it as a possible duplicate).

In my program I'm trying to run a program using a text file as it's input and redirecting the output of the program to a new file.

Here is my code :

if (fork() == 0) {
    char *args[]={"program",">","output.txt",NULL}; 
    int fd = open("/input.txt", O_RDONLY);
    dup2(fd, 0);
    execvp("program",args);

    return 0;
}

program.c is my program I'm trying to run(not my main program) /input.txt is the file I want to use as an input to my program.c and output.txt is the file I want to redirect the output of program to I know that for redirecting my program output I should use programname>outputfile.

But I can't make it work, I thing maybe I'm doing something wrong with the args array. What is the right way to sent input.txt as the input for program.c and redirect it's output to output.txt? (note that my main program is not program.c)

Any help would be appreciated

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ashly
  • 3
  • 6
  • 1
    Possible duplicate of [Using execvp with Input,Output and redirection](https://stackoverflow.com/questions/23322269/using-execvp-with-input-output-and-redirection) – Chris Turner Dec 12 '18 at 12:57
  • @ Chris Turner I mentioned in my question i tried to use it as a reference but couldn't make my program work – ashly Dec 12 '18 at 13:00
  • But you managed to use that answer to redirect the input correctly. Redirecting the output is exactly the same – Chris Turner Dec 12 '18 at 13:51
  • @Chris Turner how is it the same? im using int fd = open("/input.txt", O_RDONLY); for the input, but as far as I know the output should be used as programname>outputfile in the args array, and in the answer they were using the args array also for the input, which I was not managed to do – ashly Dec 12 '18 at 16:53
  • it is the same because you `open` a file and use `dup2` to replace stdout, exactly as the answer in the duplicate says and the answer below says too. – Chris Turner Dec 12 '18 at 17:01
  • @Chris Turner so there is no need for char *args[]={"program",">","output.txt",NULL};? – ashly Dec 12 '18 at 17:07
  • you don't need the `">","output.txt"` as it won't do what you want - as covered in the answer you've just accepted, it's a feature of the shell - the `exec` functions will just pass it as arguments to your program – Chris Turner Dec 12 '18 at 17:11

1 Answers1

0

Using programname>outputfile is a feature of the shell. The shell opens the output file for you and duplicates the file descriptor to 1 (stdout).

If you don't want to run a shell you can do it like your input redirection with open and dup2 before calling exec*. Try something like this:

int fdOut = open("output.txt", O_WRONLY | O_CREAT);
/* don't forget to check fdOut for error indication */
int rc = dup2(fdOut, 1);
/* also check the return code for errors here */
Bodo
  • 9,287
  • 1
  • 13
  • 29
  • Do you mean using your code after the dup2(fd, 0); and before execvp("program",args)? and make the char array just char *args[]={"program",NULL};? then how will the exec ? is it not going to make problems as to witch file is the input and which one is poor the output? – ashly Dec 12 '18 at 16:58
  • @ashly Yes, that's what I mean. The executed `program` will not know which input and output file it is connected to. It has to read its input from `stdin` (file descriptor `0`) and write its output to `stdout` (file descriptor `1`). This is the purpose of input and output redirection. – Bodo Dec 12 '18 at 18:13
  • think i got he idea,thank you, one more thing,if you don't mind,whenever i run the program I can't really see the output, does using "program" and not "program.c"/"program -c " is correct? or ""/input.txt" and not "input.txt"? – ashly Dec 12 '18 at 19:21
  • @ashly What do you mean with "I can't really see the output". It doesn't appear in the output file? You can't find the output file? You don't see any output in the terminal window? Your compiled program is probably named `program` if the source code is `program.c`. `/input.txt` is a file in the root directory which is probably wrong. `input.txt` is a file in the current working directory. Please check the return code of all called functions because it will tell you if something went wrong. – Bodo Dec 13 '18 at 14:27