home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19294 < prev    next >
Encoding:
Text File  |  1993-01-06  |  2.6 KB  |  60 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!mcsun!Germany.EU.net!news.Hamburg.Germany.EU.net!jwminhh!wieck
  3. From: wieck@jwminhh.hanse.de (Jan Wieck)
  4. Subject: Re: Fopening a text file
  5. Message-ID: <1993Jan6.234558.2044@jwminhh.hanse.de>
  6. Organization: Private Site under Mach386
  7. X-Newsreader: Tin 1.1 PL4
  8. References: <1993Jan5.015937.19270@vpnet.chi.il.us>
  9. Date: Wed, 6 Jan 1993 23:45:58 GMT
  10. Lines: 48
  11.  
  12. mox@vpnet.chi.il.us (William Moxley) writes:
  13. : I'm having some problems with a program that I wrote for this computer
  14. : which is running a sys V unix operating system.  My problem is with
  15. : fopen.  When I try do a fopen("filename", "rt"); it opens the file as a
  16. : binary.  If I drop the t, is still creates a binary file.  So how do I
  17. : make fopen create a straight text file?
  18. :
  19. : As you can guess, I normally don't write programs for machines other
  20. : than dos.  Thanks in advance...
  21.  
  22.         Files under UNIX are different from that under DOS in the
  23.         case  of  holding  ASCII-text. DOS (as a sibling of CP/M)
  24.         encodes a new-line as CR, LF (or 0x0D, 0x0A in HEX). UNIX
  25.         only says LF.
  26.  
  27.         This is one of the major reasons why C-programs cannot be
  28.         100%  portable between UNIX, DOS and e.g. VMS (which also
  29.         uses CR, LF).
  30.  
  31.         If your program uses fprintf(3S) and fgets(3S), it  would
  32.         get  the  same  data  back  that it wrote. But if you use
  33.         read(2) instead of fgets(3S), it possibly  wouldn't.  The
  34.         standard   I/O-functions   library   (where  the  f...()-
  35.         functions reside) is implemented under each  OS  in  that
  36.         kind,  that  a  normal  cat- | type- | show-command would
  37.         display a file, written  with  these  functions,  like  a
  38.         standard text-file under this OS.
  39.  
  40.         For this reason, the standard under DOS is to map '\n' to
  41.         0x0D,0x0A  and  vice  versa. But they wouldn't create the
  42.         same binary data as C under UNIX would  do.  To  minimize
  43.         port-overhead,  binary modes are added to these functions
  44.         under DOS. Borland's  Turbo-C  also  makes  the  same  in
  45.         read(2)  and  write(2),  so  you  have  to  use  the (TC-
  46.         specific) mode switch O_BINARY to suppress  it  (I  don't
  47.         know how other DOS compilers would do - I think similar).
  48.  
  49.         Back to you - what you read in  your  program  is  binary
  50.         data as it is text. There's no difference under UNIX.
  51.  
  52.  
  53. Until later, Jan
  54.  
  55. -- 
  56. # Any language keeps its own misunderstandings; #
  57. # why shouldn't programming languages do?       #
  58. #                                               #
  59. #            wieck@jwminhh.hanse.de (Jan Wieck) #
  60.