home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / unix / question / 13291 < prev    next >
Encoding:
Text File  |  1992-11-12  |  1.3 KB  |  32 lines

  1. Newsgroups: comp.unix.questions
  2. Path: sparky!uunet!ukma!darwin.sura.net!jvnc.net!princeton!tex.Princeton.EDU!subbarao
  3. From: subbarao@fc.hp.com (Kartik Subbarao)
  4. Subject: Re: Proper way to read/write a struct from/to a socket
  5. Message-ID: <1992Nov12.151114.3244@Princeton.EDU>
  6. Sender: news@Princeton.EDU (USENET News System)
  7. Nntp-Posting-Host: tex.princeton.edu
  8. Reply-To: subbarao@fc.hp.com
  9. Organization: putchar('I'); for (i = 0; i < 3; i++) putchar('E');
  10. References: <60p137_@rpi.edu>
  11. Date: Thu, 12 Nov 1992 15:11:14 GMT
  12. Lines: 18
  13.  
  14. In article <60p137_@rpi.edu> cecchinr@gehrig.cs.rpi.edu (Ron Cecchini) writes:
  15. >Hi all,
  16. >
  17. >o read()/write() expect you to read/write a "char *buf"
  18. >o I have a variable "foo" which is of type "struct foo_type"
  19. >o I've been reading/writing "(char *)&foo"
  20. >
  21. >It seems to be working.  Am I just lucky, or is this correct?
  22. >I've never tried reading into a structure like this before...
  23.  
  24. Sure, it's fine. Think about it. The read() and write() calls transfer
  25. some bytes from where you tell it to start. C stores the elements 
  26. in a struct contiguously in memory, so when you give the address of the struct
  27. as the starting address, then say sizeof struct for the number of bytes to
  28. be sent, and assuming the other end has the same byte-ordering scheme, then
  29. you can read it right into another struct.
  30.  
  31.     -Kartik
  32.