The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
writing to a file amitrajvarma Shell Programming and Scripting 1 03-09-2008 10:29 PM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 01:38 PM
writing to a file using awk gfhgfnhhn Shell Programming and Scripting 7 09-15-2006 06:52 AM
Writing to another file dreams5617 Shell Programming and Scripting 3 07-05-2006 10:39 AM
Writing to a file in different way dhananjaysk Shell Programming and Scripting 4 04-11-2006 08:54 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 03-29-2008
Registered User
 

Join Date: Mar 2008
Posts: 1
Writing from STDIN_FILENO to a file

Hey,
Im a complete noob in UNIX and this problem is killing me.
Im trying to write the stdin (which i receive from a pipe) to a file, but as always C crashes without no explanation. Here is what i have so far:
Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
   pid_t childpid;
   int fd[2];
   int file;
   mode_t fd_mode=S_IRWXU;
   if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
      perror("Failed to setup pipeline");
      return 1;
   }
   if(childpid>0) {                                  
        // send output to the pipe
      printf("I am parent\n");
      if (dup2(fd[1], STDOUT_FILENO) == -1)
         perror("Failed to redirect stdout of ls");
      else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
         perror("Failed to close extra pipe descriptors on ls");
      else {
         execl("input",NULL);   // input is an executable which produces 
// 2 lines of text
         perror("Failed to exec output file");
      }
      return 1;          
   }
        // receive input from the pipe
   printf("I am child\n");
   if (dup2(fd[0], STDIN_FILENO) == -1)              
       perror("Failed to redirect stdin of writing to file");
   else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
       perror("Failed to close extra pipe file descriptors");
  else {
      if((file=open("file1.txt",O_WRONLY | O_CREAT,fd_mode))==-1)
         perror("Error opening the file");
      char *buffer=(char *)malloc(200);  
      fgets(buffer,100,STDIN_FILENO);    // this should in theory write 100
// characters to the buffer from stdin, but it kills the program
      write(file,buffer,100);
      perror("Failed to write to the pipe");
   }
   return 1;
}
Thanx...
Reply With Quote
Forum Sponsor
  #2  
Old 03-31-2008
andryk's Avatar
Registered User
 

Join Date: Sep 2003
Posts: 448
Quote:
Originally Posted by alexxz4 View Post
Hey,
Im a complete noob in UNIX and this problem is killing me.
Im trying to write the stdin (which i receive from a pipe) to a file, but as always C crashes without no explanation. Here is what i have so far:
Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(void) {
   pid_t childpid;
   int fd[2];
   int file;
   mode_t fd_mode=S_IRWXU;
   if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
      perror("Failed to setup pipeline");
      return 1;
   }
   if(childpid>0) {                                  
        // send output to the pipe
      printf("parent(%d)\n",getpid());
      if (dup2(fd[1], STDOUT_FILENO) == -1)
         perror("Failed to redirect stdout of ls");
      else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
         perror("Failed to close extra pipe descriptors on ls");
      else {
         execl("input",NULL);   // input is an executable which produces 
// 2 lines of text
         perror("Failed to exec output file");
      }
     waitpid();
      return 1;          
   }
        // receive input from the pipe
   printf("I am child\n");
   if (dup2(fd[0], STDIN_FILENO) == -1)              
       perror("Failed to redirect stdin of writing to file");
   else if ((close(fd[0]) == -1) || (close(fd[1]) == -1))
       perror("Failed to close extra pipe file descriptors");
  else {
      if((file=open("file1.txt",O_WRONLY | O_CREAT,fd_mode))==-1)
         perror("Error opening the file");
      char *buffer=(char *)malloc(200); 
      int n; 
      memset(buffer,0,200);
      if ((n=read(STDIN_FILENO,buffer,200))<=0) {
         perror("read");
      }
      printf("child(%d)>I got %s\n",getpid(),buffer);
      //fgets(buffer,100,STDIN_FILENO);    // this should in theory write 100
// characters to the buffer from stdin, but it kills the program
      write(file,buffer,n);
   }
   return 1;
}
Thanx...
Hi,
try the modified code above and report back the result ... it seems to work fine for me
Reply With Quote
  #3  
Old 04-01-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Question

What exactly are you trying to do? Looks like you want to use pipes to communicate between the parent and child process. Standard output (stdout) of the parent process (ls) goes to the pipe which becomes the standard input (stdin) of the child process (sort). stdout of the child process...sort in this case...goes to a file.

ls | sort > file.out

Or are you sending the stdout of the parent process to a file which then becomes the stdin of the child process. Or if it's neither of the above. Be specific as to the one that best fits your needs.

ls > file.out
sort < file.out

Reply With Quote
  #4  
Old 04-03-2008
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 567
Changes to the original source code are highlighted in red.

Code:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void) {
   pid_t childpid;
   int fd[2];
   int file;
   mode_t fd_mode=S_IRWXU;

   if ((pipe(fd) == -1) || ((childpid = fork()) == -1)) {
       perror("Failed to setup pipeline");
       return 1;
   }
   if(childpid>0) {
        /* send output to the pipe */
      printf("I am parent\n");
      if (dup2(fd[1], STDOUT_FILENO) == -1) {
          perror("Failed to redirect stdout of ls");
          return 1;
      }
      else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
               perror("Failed to close extra pipe descriptors on ls");
               return 1;
      } else
          if (execl("/bin/ls", "ls", "-lrt", (char *) 0) < 0) {
              perror("Failed to exec output file");
              return 1;          
         }
   }
        /* receive input from the pipe */
   printf("I am child\n");
   if (dup2(fd[0], STDIN_FILENO) == -1) {
       perror("Failed to redirect stdin of writing to file");
       return 1;
   }
   else if ((close(fd[0]) == -1) || (close(fd[1]) == -1)) {
            perror("Failed to close extra pipe file descriptors");
            return 1;
   } else {
       if ((file=open("file1.txt",O_WRONLY | O_CREAT,fd_mode))==-1) {
           perror("Error opening the file");
           return 1;
       }
       char *buffer=(char *)malloc(200);
/*
 * use read() instead of fgets() as read() takes an int for the file
 * descriptor while fgets() takes a pointer to a stream of type FILE.
 * Replacing fgets() with read() doesn't crash the program anymore.
 */
       read(STDIN_FILENO, buffer, 100);
       if (write(file, buffer, 100) < 0) {
           perror("Failed to write to the pipe");
           return 1;
       }
   }
   return 0;
}
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 04:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0