home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.questions
- Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!public.btr.com!rem
- From: rem@public.btr.com (Robert E. Maas rem@btr.com)
- Subject: Novice programming question: How to read from pipes?
- Message-ID: <9212300507.AA08805@public.btr.com.BTR.COM>
- Sender: daemon@cis.ohio-state.edu
- Organization: The Ohio State University Department of Computer and Information Science
- Date: Wed, 30 Dec 1992 05:07:17 GMT
- Lines: 36
-
- I'm just starting programming on Unix, trying various things, having
- trouble reading from pipes. The following program seems to create a
- pipe and write data to one end, but I'm not sure because it bombs out
- with "Segmentation fault" when it tries to read that data from the
- other end of the the pipe. Anybody want to give me a clue what's wrong
- (Please e-mail to rem@BTR.Com)?
-
- Here's the complete test-program, and what happens when I run it (this
- is on SunOS 4.1.1, if that makes any difference):
-
- #include <stdio.h>
- main()
- {printf("Test rig...\n");
- tryPipe();
- printf("\nAll Done.\n");
- }
-
- tryPipe()
- {int fd[2],res,ch;
- printf("Making a pipe ...\n");
- res = pipe(fd);
- printf("res=%d fd = %o %o\n",res,fd[0],fd[1]);
- printf("Writing some bytes ...\n");
- fputc('Q',&fd[1]); /* Just one byte until I get it working */
- printf("Reading the data back in ...\n");
- ch=fgetc(&fd[0]);
- printf("<<%c>>\n",ch);
- }
-
-
- Test rig...
- Making a pipe ...
- res=0 fd = 3 4
- Writing some bytes ...
- Reading the data back in ...
- Segmentation fault (core dumped)
-