file output redirect from child

This is a discussion on file output redirect from child within the C Programming forums, part of the General Programming Boards category; In a main process I create a child process which runs a converter. The result of the converter normally outputs ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    file output redirect from child

    In a main process I create a child process which runs a converter. The result of the converter normally outputs to the screen. This result has to be redirected to a file is some directory. Normally you should run following command: progname file2 > file3. In a main console no problem. Giving this command to a child process progname considers instead of 1 parameter three parameters [1] file2 [2] > [3] file3 and gives the error to many parameters as it expects only one.

    The code below a bit abstract, but as it is company I can not give all code, will try to shorten a set only the essentials.:

    Open FILE * out for “w”
    Dup2(out,1)
    Start program in fork() /in child process close(out)
    Wait(child finish)s

    The result is that the file is created, output comes to screen and the file stays empty.
    Last edited by rvervecken; 08-12-2009 at 10:15 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The following worked for me:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    #include <fcntl.h>
    #include <stdlib.h>
    int main() {
    
        int out;
        out = open("redirection", O_WRONLY|O_CREAT);
        dup2(out, 1);
        if (0 == fork()) {
            system("./bools");
            close(out);
        } else {
            wait(0);
        }
        return 0;
    }
    (bools was just a program I had sitting around that did stuff.) If you're mixing FILE * and dup2, that sounds like a bad idea, but maybe I just don't know how well that works.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    3

    shortened code of the problem.

    Here the code for the above problem. Took out all unnecessary balast. it wouldn't even create the file in the first place.

    desti_dir: destination directory
    source_dir: source directory.

    Thanks for anybody's help. It's frustating if it does work after half a day.

    Code:
    int do_process(QUALIFIERS * in, char * mode)  ---->>> called from main
    {
      int status, i;
      char    t_command[250];
      int commandlen;
      int out; 
      char out_file[50];
    
      sprintf(out_file,"%s/%s",in->dest_dir,"file03567.redir");
      out = open(out_file[0], O_WRONLY|O_CREAT);
       sprintf(t_command,"Converter k %s/%s ", in->source_dir,"file03567");
    
      dup2(out, 1); // redirect output to the file
      status=RW_startProgram(getenv("EXE"),&t_command[0]);
    
      wait(&status);
    
      return SUCCESS;
    
    int RW_startProgram( char* path, char* commandline )
    {
      /**************************************************************/
      /*                                                            */
      /* start program, where                                       */
      /*       path: path of the executable program in commandline  */ 
      /*       commandline: command line string containing          */
      /*                    executable program with zero, one or    */
      /*                    more arguments                          */
      /*                                                            */
      /* returns:                                                   */
      /*        =-2 invalid command line,can not be splitted        */
      /*        =-1 can not start child process                     */
      /*            or fork error                                   */
      /*        =mypid successfully                                 */
      /**************************************************************/
      char *argv[100];
      int argc=0;
      char start_path[PATH_MAX];
      int  my_pid;
      /* first get command line and split into argv      */
      /* and get number of command line arguments        */
      /* path like /appl/chprd/exe/"                     */
      argc = CHsplitCommandLine(commandline, argv, 100); --->> splits commandline in arguments  
    if(argc <= 0 )
        {
          trace("pe","invalid command line"); --> trace is a routine to print msg with timestamp      return (-2); 
        }
      
      /* prepare arguments for call execv(path,argv),    */
      /* where argv must be terminated with NULL pointer */
       	sprintf(start_path,"%s%s", path, argv[0]);
      /* let's have some children */
      /* sleep random time to avoid conflicts */
      CHrandomSleep();
      fflush(stderr);
      
      switch ( my_pid = (int)fork() )
        {
        case -1:  /* fork failure, start alarm */
          return(-1);
          
        case  0:  /* this is the child process */      
          errno=0;
           execv(start_path, argv);
          /* if process successful started, program end hier */
          
          trace("pe","CHILD pid %d: Unsuccesfull exec. Errno :%d  -- %s",(int)getpid(), errno,strerror(errno));
          fflush (stderr); 
          exit(-1); 
          
        default:  /* this is the parent process */
          break;
      	}
      
      /* if disconnected before fork do this:		   */
      wait(&my_pid);
    
      trace("pi","              End of start command (pid: %d, parent %d)",getpid(),getppid());
      return(my_pid);
    }

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    char out_file[50];
    ...
    out = open(out_file[0], O_WRONLY|O_CREAT);
    You should be passing open() a pointer to the start of the string, but what you are passing is the first character of that string.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    3
    @tabstop:

    the int out always has a value of -1 and the file is not created.

    @Sean

    tried all possible ways (out_file, &out_file, &out_file[0]) all with the same result. same value as with tabstop's program.

    Thanks anyway for trying. Don't know where that bug is. Normally at least the files should have been created.

    All was done on a SOLARIS 10
    Last edited by rvervecken; 08-12-2009 at 11:46 AM. Reason: included message to Sean./added OS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 04:44 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 04:39 AM
  4. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 02:11 AM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21