-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

7
  • 1
    Possible duplicate of Using execvp with Input,Output and redirection Commented Dec 12, 2018 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 Commented Dec 12, 2018 at 13:00
  • But you managed to use that answer to redirect the input correctly. Redirecting the output is exactly the same Commented Dec 12, 2018 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 Commented Dec 12, 2018 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. Commented Dec 12, 2018 at 17:01

1 Answer 1

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 */
Sign up to request clarification or add additional context in comments.

4 Comments

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

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.