home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.unix.programmer
- Path: sparky!uunet!news.claremont.edu!ucivax!orion.oac.uci.edu!unogate!stsshol
- From: stsshol@st.unocal.COM (Herb Lam)
- Subject: Re: Problem reading from pipe ("|") using read() -- Solved
- Message-ID: <1992Aug26.195859.10186@unocal.com>
- Sender: news@unocal.com (Unocal USENET News)
- Organization: Unocal Corporation
- Date: Wed, 26 Aug 1992 19:58:59 GMT
- Lines: 71
-
- First of all, thanks to those who tried to help even thought I didn't
- provide enough info in my original post. Thanks to David Wagner who
- provided a subroutine for reading and checking the return value.
-
- I have fixed the problem so that I can read from the pipe, but I don't
- understand exactly why it works. Here is my modification of David's
- read routine that I used to read data:
-
- size_t tp_read(int fd, char *bufptr, size_t count)
- {
- size_t nread = 0;
- int i;
-
- for (i = 0; i < count; i += nread) {
- nread = read(fd, bufptr + i, count - i);
- if (nread < 0)
- err("read: %s", sys_errlist[errno]);
- else if (nread == 0)
- break;
- }
-
- return nread;
- }
-
- The data that I'm reading is in this format: ascii header, binary header,
- binary data. The binary data is broken into parts of equal sizes; i.e.,
- the first data part contains 2000 bytes, the second part 2000 bytes, etc.
- The data part can be from ~250 bytes to ~64kb.
-
- Now the part that I don't understand is this. I read each section as
- follows:
-
- nread = tp_read(fd, &ascii_header, ascii_bytes);
- ...
- nread = tp_read(fd, &binary_header, binary_bytes);
- ...
- while(...) {
- nread = tp_read(fd, &data, data_bytes);
- ...
- }
-
- Apparently, the program had tp_read(fd, (char *)&data, data_bytes).
- When I took out the cast, the program worked. However, the original
- program also cast the ascii_header and binary_header to (char *);
- but I was always able to read the headers correctly. I won't list out
- the entire "data" structure since it is very long, but its members are
- of type short, unsigned short, long, and float. The ascii_header and
- binary_header have similar members.
-
- I also tried replacing tp_read() with the system read() after I got
- the program to work; however, I was not able to read the data correctly.
- The headers come out fine though. As I put it in the other post, the
- data appears "all messed up." What I mean by this is that I can't tell
- from the output how the data is being read. It doesn't appear to have
- been offset. It just looks like garbage.
-
- If I go back and use the tp_read subroutine and check how many times
- it went through the loop, everytime I checked the value came out to be
- 1. So it seems that it was able to read all the data on the first try.
- Why doesn't it work if I just put read() in place of tp_read()?
-
-
-
-
-
-
-
-
-
-
-
-