dup()

when i want to replace standard output with output file

int out;

out = open("out", O_WRONLY)p;
dup2(out,1);

What Shall I do in case of appending??? I am using here O_WRONLY TO WRITE.BUT IF i wanna append, whats the word?

Open the file with O_WRONLY|O_APPEND

I have tried dup by itself in a little program its working. But when am using it in my original program its hanging...

// included everything needed. sys/types.h fcntl.h ...
int in, out, out1;

int doable(char Buffer[], char *args[], int *back_ground)
{
         //------watever here...

           // length is length of the Buffer.
           for(i = 0; i<length;i++)
           {
                   switch(Buffer)
                   {
                          case '>':
                              argument[0] = &Buffer[i+2];//consider &buffer[i+1] space . the user will write ls > myfile
                              out = open (argument[0] , O_WRONLY 
O_CREAT);
                              dup2(out,1);
                              i = length+1;
                   }
              }
}



int main()

{
int x1 = 0;
int back_ground = 0;
while(1)
{
      x1 = doable(Buffer, arguments, &back_ground)
      child = fork()
      if(child == 0)
      {      // i call execvp
       }
      else
       {
               // if backgroun or not.and i do the wait..and return if no error from execvp.(i       use       waitpid)
        }

// after both parent & child finish
       close(out);
      printf("new command line");
}}

The program is working with me without adding the dup and stuff.But after I added the dup & stuff..Whenever I run command ls > myfile ...the program hangs..and in my file nthg is printed...

The shell is going to process that > for you and open the file before your program even begins to run. Similarly, >> would cause the shell to append for you. Surround them in single quotes like '>' to tell the shell not to process it for you.

Beyond that I can't tell what it's doing at all unless you post your actual program, not a mock-up.

I sent you my whole program as a message, I prefer not to post here my complete program.

Afraid some people from my class find my code..

Joey -
Homework and classwork are not allowed on the Forums. Sorry. Thread closed.