3 Replies - 224 Views - Last Post: 07 November 2017 - 10:53 AM Rate Topic: -----

#1 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 135
  • Joined: 26-March 14

Redirection in C

Posted 07 November 2017 - 09:48 AM

so im trying to do input/output redirect so if i was to type "cat > test.txt" it would create the file if it doesn't exist.

So in the child process i'm trying this:

                                        {
                                         if((!strcmp(args[t], ">"))) //write create if doesn't exist truncate if does
                                         {
                                                close(fd[0]);
                                                close(1);
                                                myFile = fopen(args[t+1], "w+");
                                                dup2(fileno(myFile) , STDIN_FILENO);
                                         }
                                         if((!strcmp(args[t], "<")))
                                         {
                                                if(access(args[t+1], F_OK))
                                                                {
                                                                myFile = freopen(args[t+1], "r", stdout);
                                                                dup2(fileno(myFile),STDOUT_FILENO);
                                                                fclose(myFile);
                                                                exit(-1);
                                                                }
                                                //args[1] = args[t+1];
                                                //args[2] = "<";
                                         }
                                          t++;
                                        }
                                        result = execvp(args[0], args);
                                        fclose(myFile);
                                        _exit(127);




But i keep getting error " > no such file or directory. I'm not exactly sure what i'm doing wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: Redirection in C

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Redirection in C

Posted 07 November 2017 - 10:15 AM

Which line of code is giving you that error?
Was This Post Helpful? 0
  • +
  • -

#3 Joshieboy2007  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 135
  • Joined: 26-March 14

Re: Redirection in C

Posted 07 November 2017 - 10:45 AM

I think the error occurse at the execvp() portion.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5899
  • View blog
  • Posts: 20,142
  • Joined: 05-May 12

Re: Redirection in C

Posted 07 November 2017 - 10:53 AM

I don't think you are allowed to pass redirection characters to the exec() family of functions.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1