home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / lang / c / 20249 < prev    next >
Encoding:
Text File  |  1993-01-27  |  2.4 KB  |  62 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!gatech!swrinde!zaphod.mps.ohio-state.edu!moe.ksu.ksu.edu!usenet-feed.umr.edu!mcastle
  3. From: mcastle@cs.umr.edu (Michael R Castle)
  4. Subject: Re: How do you read binary data piped into stdin?
  5. References: <N0gRXB1w165w@eastwind.mcds.com>
  6. Date: Tue, 26 Jan 1993 17:38:17 GMT
  7. Nntp-Posting-Host: next9.cs.umr.edu
  8. Organization: University of Missouri - Rolla, Rolla, MO
  9. Keywords: stdin, redirection, binary data
  10. Sender: cnews@umr.edu (UMR Usenet News Post)
  11. Message-ID: <1993Jan26.173817.1397@umr.edu>
  12. Lines: 48
  13.  
  14. In article <N0gRXB1w165w@eastwind.mcds.com> chorn@eastwind.mcds.com writes:
  15. >
  16. >I'm relatively new to C, so still have a lot to learn, so I apologize
  17. >if this is a silly question. I need to read raw binary data from
  18. >stdin (it is being piped in) but it appears stdin operates in text
  19. >mode. The first ascii 26 that comes along triggers end of file, and
  20. >I can't read the rest of the data. Could someone please explain, or
  21. >better yet show me a short peice of code, that could do this? The
  22. >answer is probably obvious, but it eludes me at the moment. Thanks
  23. >a lot!
  24.  
  25. After reading the followups for this article, I'm surprised no one has 
  26. asked the obvious "What system are you on?"
  27.  
  28. The only system I've come across where ascii 26 is special is MS-DOS 
  29. (well, ok, OS/2 as well).  
  30.  
  31. If it is ms-dos, please specify.  Knowing what system you are using 
  32. (especially for very system dependent things such as file i/o), helps
  33. tremendously in helping solve the problem. 
  34.  
  35. Anyway, if it IS really ms-dos, the following will work for turbo-c
  36. and djgpp (gcc/g++ ported to dos).  I believe it will work with ms-c
  37. as well, but not sure.
  38.  
  39. #include <fnctl.h>
  40.  
  41. setmode(fileno(stdin),O_BINARY);
  42.  
  43. This will change stdin (or any open file) to binary mode.
  44.  
  45. You can use this to change back and forth between binary/text mode if you
  46. so wish.
  47.  
  48. Btw, when trying to help a friend port pbmplus to os/2, this solution 
  49. did NOT work because setmode() would not affect a stream (ie, stdin,
  50. stdout, i guess anything opened with fopen()).  T'was more than a bit
  51. annoying (oh, with ibm c-set/2).  
  52.  
  53. hope this helps.
  54.  
  55. regards,
  56. mrc
  57. -- 
  58. Mike Castle .-=NEXUS=-.  Life is like a clock:  You can work constantly
  59.   mcastle@cs.umr.edu     and be right all the time, or not work at all
  60. S087891@UMRVMA.UMR.EDU   and be right at least twice a day.  -- mrc
  61.     We are all of us living in the shadow of Manhattan.  -- Watchmen
  62.