home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / unix / question / 15039 < prev    next >
Encoding:
Text File  |  1992-12-30  |  1.5 KB  |  47 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!public.btr.com!rem
  3. From: rem@public.btr.com (Robert E. Maas  rem@btr.com)
  4. Subject: Novice programming question: How to read from pipes?
  5. Message-ID: <9212300507.AA08805@public.btr.com.BTR.COM>
  6. Sender: daemon@cis.ohio-state.edu
  7. Organization: The Ohio State University Department of Computer and Information Science
  8. Date: Wed, 30 Dec 1992 05:07:17 GMT
  9. Lines: 36
  10.  
  11. I'm just starting programming on Unix, trying various things, having
  12. trouble reading from pipes. The following program seems to create a
  13. pipe and write data to one end, but I'm not sure because it bombs out
  14. with "Segmentation fault" when it tries to read that data from the
  15. other end of the the pipe. Anybody want to give me a clue what's wrong
  16. (Please e-mail to rem@BTR.Com)?
  17.  
  18. Here's the complete test-program, and what happens when I run it (this
  19. is on SunOS 4.1.1, if that makes any difference):
  20.  
  21. #include <stdio.h>
  22. main()
  23.   {printf("Test rig...\n");
  24.    tryPipe();
  25.    printf("\nAll Done.\n");
  26.    }
  27.  
  28. tryPipe()
  29.   {int fd[2],res,ch;
  30.    printf("Making a pipe ...\n");
  31.    res = pipe(fd);
  32.    printf("res=%d fd = %o %o\n",res,fd[0],fd[1]);
  33.    printf("Writing some bytes ...\n");
  34.    fputc('Q',&fd[1]); /* Just one byte until I get it working */
  35.    printf("Reading the data back in ...\n");
  36.    ch=fgetc(&fd[0]);
  37.    printf("<<%c>>\n",ch);
  38.    }
  39.  
  40.  
  41. Test rig...
  42. Making a pipe ...
  43. res=0 fd = 3 4
  44. Writing some bytes ...
  45. Reading the data back in ...
  46. Segmentation fault (core dumped)
  47.