home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / lang / c / 19232 < prev    next >
Encoding:
Internet Message Format  |  1993-01-05  |  3.2 KB

  1. Path: sparky!uunet!ogicse!news.u.washington.edu!cpac.washington.edu!mcglk
  2. From: mcglk@cpac.washington.edu (Ken McGlothlen)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Fopening a text file
  5. Message-ID: <MCGLK.93Jan5005103@yang.cpac.washington.edu>
  6. Date: 5 Jan 93 08:51:03 GMT
  7. Article-I.D.: yang.MCGLK.93Jan5005103
  8. References: <1993Jan5.015937.19270@vpnet.chi.il.us>
  9. Distribution: usa
  10. Organization: Dubious.
  11. Lines: 91
  12. NNTP-Posting-Host: yang.cpac.washington.edu
  13.  
  14. William Moxley (mox@vpnet.chi.il.us) writes:
  15. +----------
  16. | When I try do a fopen("filename", "rt"); it opens the file as a binary.  If I
  17. | drop the t, is still creates a binary file.  So how do I make fopen create a
  18. | straight text file?  [...]  I normally don't write programs for machines
  19. | other than dos.
  20. +----------
  21.  
  22. I hate to tell you this, but the ugly truth is:
  23.  
  24.     Unix only has one kind of file.  The binary file.
  25.  
  26. MS-DOS, designed with all the force of Microsoft's wisdom, chose to represent
  27. the end-of-line character with two characters:  ^M (ASCII 13, or CR, or \r in C
  28. parlance) followed by ^J (ASCII 10, or LF, or \n in C parlance).  However, Unix
  29. systems only use LF to determine end-of-line.  So in DOS, when you do
  30.  
  31.     fprintf( textfile, "%s\n", "This is a line." );
  32.  
  33. on a file opened with the "wt" mode of fopen(), you get, in ASCII,
  34.  
  35.     This is a line.\r\n
  36.  
  37. Unix, however, only gives you
  38.  
  39.     This is a line.\n
  40.  
  41. DOS will give you the same if you open the file with just "w" as the mode; just
  42. the LF instead of the CR-LF pair.
  43.  
  44. MS-DOS chose to make things a bit more complicated, and normally, this wouldn't
  45. be a big problem, except that you're trying to get a file to run under Unix
  46. which produces files you want to move to the DOS system.  And so your life
  47. becomes messy.  However, there are a couple of easy scripts you can use, which
  48. I've appended to this message, which will convert back and forth between Unix
  49. text files and DOS text files.  Good luck.
  50.  
  51.                             ---Ken
  52.  
  53. #! /bin/sh
  54. # This is a shell archive, meaning:
  55. # 1. Remove everything above the #! /bin/sh line.
  56. # 2. Save the resulting text in a file.
  57. # 3. Execute the file with /bin/sh (not csh) to create:
  58. #    unix2dos
  59. #    dos2unix
  60. # This archive created: Tue Jan  5 00:50:17 1993
  61. # By:    Ken McGlothlen
  62. export PATH; PATH=/bin:/usr/bin:$PATH
  63. echo shar: "extracting 'unix2dos'" '(131 characters)'
  64. if test -f 'unix2dos'
  65. then
  66.     mv unix2dos unix2dos~; echo shar: "moved unix2dos to unix2dos~"
  67. fi; if test -d . ; then
  68. sed 's/^X//' << \OogaBooga > 'unix2dos'
  69. X#!/bin/sh
  70. X#
  71. X#    Converts Unix text files to something more friendly to DOS, by adding
  72. X#    CRs to the ends of each line.
  73. X
  74. Xsed 's/$/
  75. X
  76. OogaBooga
  77. if test 131 -ne "`wc -c < 'unix2dos'`"
  78. then
  79.     echo shar: "error transmitting 'unix2dos'" '(should have been 131 characters)'
  80. fi
  81. chmod 700 'unix2dos'
  82. fi
  83. echo shar: "extracting 'dos2unix'" '(129 characters)'
  84. if test -f 'dos2unix'
  85. then
  86.     mv dos2unix dos2unix~; echo shar: "moved dos2unix to dos2unix~"
  87. fi; if test -d . ; then
  88. sed 's/^X//' << \OogaBooga > 'dos2unix'
  89. X#!/bin/sh
  90. X#
  91. X#    Converts a DOS text file to something more Unixy, by stripping the
  92. X#    CRs off the ends of the lines.
  93. X
  94. Xsed 's/
  95. X
  96. OogaBooga
  97. if test 129 -ne "`wc -c < 'dos2unix'`"
  98. then
  99.     echo shar: "error transmitting 'dos2unix'" '(should have been 129 characters)'
  100. fi
  101. chmod 700 'dos2unix'
  102. fi
  103. exit 0
  104. #    End of shell archive
  105.