home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / unix / programm / 4456 < prev    next >
Encoding:
Text File  |  1992-08-26  |  2.8 KB  |  82 lines

  1. Newsgroups: comp.unix.programmer
  2. Path: sparky!uunet!news.claremont.edu!ucivax!orion.oac.uci.edu!unogate!stsshol
  3. From: stsshol@st.unocal.COM (Herb Lam)
  4. Subject: Re: Problem reading from pipe ("|") using read() -- Solved
  5. Message-ID: <1992Aug26.195859.10186@unocal.com>
  6. Sender: news@unocal.com (Unocal USENET News)
  7. Organization: Unocal Corporation
  8. Date: Wed, 26 Aug 1992 19:58:59 GMT
  9. Lines: 71
  10.  
  11. First of all, thanks to those who tried to help even thought I didn't 
  12. provide enough info in my original post.  Thanks to David Wagner who
  13. provided a subroutine for reading and checking the return value.
  14.  
  15. I have fixed the problem so that I can read from the pipe, but I don't
  16. understand exactly why it works.  Here is my modification of David's 
  17. read routine that I used to read data:
  18.  
  19.     size_t tp_read(int fd, char *bufptr, size_t count)
  20.     {    
  21.         size_t nread = 0;
  22.         int    i;
  23.  
  24.         for (i = 0; i < count; i += nread) {
  25.         nread = read(fd, bufptr + i, count - i);
  26.         if (nread < 0) 
  27.                 err("read: %s", sys_errlist[errno]);
  28.             else if (nread == 0)
  29.                 break;                      
  30.         }        
  31.  
  32.         return nread;
  33.     }
  34.  
  35. The data that I'm reading is in this format: ascii header, binary header,
  36. binary data.  The binary data is broken into parts of equal sizes; i.e., 
  37. the first data part contains 2000 bytes, the second part 2000 bytes, etc.  
  38. The data part can be from ~250 bytes to ~64kb.
  39.  
  40. Now the part that I don't understand is this.  I read each section as 
  41. follows:
  42.     
  43.     nread = tp_read(fd, &ascii_header, ascii_bytes);
  44.     ...
  45.     nread = tp_read(fd, &binary_header, binary_bytes);
  46.     ...
  47.     while(...) {
  48.       nread = tp_read(fd, &data, data_bytes);
  49.       ...
  50.     }
  51.  
  52. Apparently, the program had tp_read(fd, (char *)&data, data_bytes).  
  53. When I took out the cast, the program worked.  However, the original
  54. program also cast the ascii_header and binary_header to (char *); 
  55. but I was always able to read the headers correctly.  I won't list out 
  56. the entire "data" structure since it is very long, but its members are 
  57. of type short, unsigned short, long, and float.  The ascii_header and
  58. binary_header have similar members.
  59.  
  60. I also tried replacing tp_read() with the system read() after I got
  61. the program to work; however, I was not able to read the data correctly.  
  62. The headers come out fine though.  As I put it in the other post, the 
  63. data appears "all messed up."  What I mean by this is that I can't tell 
  64. from the output how the data is being read.  It doesn't appear to have 
  65. been offset.  It just looks like garbage.
  66.  
  67. If I go back and use the tp_read subroutine and check how many times
  68. it went through the loop, everytime I checked the value came out to be 
  69. 1.  So it seems that it was able to read all the data on the first try.  
  70. Why doesn't it work if I just put read() in place of tp_read()?
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.