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-10-2008 01:29 AM
Reading a file and writing the file name to a param file. thebeginer UNIX for Advanced & Expert Users 1 10-05-2007 05:38 PM
writing to a file using awk gfhgfnhhn Shell Programming and Scripting 7 09-15-2006 10:52 AM
Writing to another file dreams5617 Shell Programming and Scripting 3 07-05-2006 02:39 PM
Writing to a file in different way dhananjaysk Shell Programming and Scripting 4 04-11-2006 12:54 PM

 
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Prev Previous Post   Next Post Next
  #1 (permalink)  
Old 03-29-2008
alexxz4 alexxz4 is offline
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...
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:40 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0