c++ - creating file for shell output redirection -


this school assignment.

so have written c code shell handles terminating command &, , input / output redirection using > or <. using pipes , fork(), execvp().

the problem input/output redirection handles files already exist.

what need know how go redirecting output file doesn't exist - know have create file, i'm not sure how works.

for example: ls -a < test.txt

if test.txt not in directory, need create , redirect output that. how create file?

here basic example code not create new file:

        else if( '>' ==  buff[i] ){            i++;            j=0;             if( ' ' == buff[i] )                i++;             while( ' ' != buff[i] && < len )               out_file[j++]=buff[i++];            out_file[j]='\0';              if ( ( ofd = open(out_file,1) ) < 0 ){               perror("output redirected file");               exit( 1 );            }             close(1);            dup(ofd);         } 

any how can output , create new file appreciated! thanks!

you need tell open create file, if necessary:

if ( ( ofd = open(out_file, o_wronly | o_creat) ) < 0 ) { 

note code more readable if use named constants flags open. compare functionally equivalent

if ( ( ofd = open(out_file, 513) ) < 0 ) { 

or even

if ( ( ofd = open(out_file, 0x0201) ) < 0 ) { 

Comments

Popular posts from this blog

iphone - Three second countdown in cocos2d -

hyperlink - how to do url routing in php -

c - Avoiding Extra Malloc in Linked List (node->next = NULL) -