home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!ogicse!news.u.washington.edu!cpac.washington.edu!mcglk
- From: mcglk@cpac.washington.edu (Ken McGlothlen)
- Newsgroups: comp.lang.c
- Subject: Re: Fopening a text file
- Message-ID: <MCGLK.93Jan5005103@yang.cpac.washington.edu>
- Date: 5 Jan 93 08:51:03 GMT
- Article-I.D.: yang.MCGLK.93Jan5005103
- References: <1993Jan5.015937.19270@vpnet.chi.il.us>
- Distribution: usa
- Organization: Dubious.
- Lines: 91
- NNTP-Posting-Host: yang.cpac.washington.edu
-
- William Moxley (mox@vpnet.chi.il.us) writes:
- +----------
- | When I try do a fopen("filename", "rt"); it opens the file as a binary. If I
- | drop the t, is still creates a binary file. So how do I make fopen create a
- | straight text file? [...] I normally don't write programs for machines
- | other than dos.
- +----------
-
- I hate to tell you this, but the ugly truth is:
-
- Unix only has one kind of file. The binary file.
-
- MS-DOS, designed with all the force of Microsoft's wisdom, chose to represent
- the end-of-line character with two characters: ^M (ASCII 13, or CR, or \r in C
- parlance) followed by ^J (ASCII 10, or LF, or \n in C parlance). However, Unix
- systems only use LF to determine end-of-line. So in DOS, when you do
-
- fprintf( textfile, "%s\n", "This is a line." );
-
- on a file opened with the "wt" mode of fopen(), you get, in ASCII,
-
- This is a line.\r\n
-
- Unix, however, only gives you
-
- This is a line.\n
-
- DOS will give you the same if you open the file with just "w" as the mode; just
- the LF instead of the CR-LF pair.
-
- MS-DOS chose to make things a bit more complicated, and normally, this wouldn't
- be a big problem, except that you're trying to get a file to run under Unix
- which produces files you want to move to the DOS system. And so your life
- becomes messy. However, there are a couple of easy scripts you can use, which
- I've appended to this message, which will convert back and forth between Unix
- text files and DOS text files. Good luck.
-
- ---Ken
-
- #! /bin/sh
- # This is a shell archive, meaning:
- # 1. Remove everything above the #! /bin/sh line.
- # 2. Save the resulting text in a file.
- # 3. Execute the file with /bin/sh (not csh) to create:
- # unix2dos
- # dos2unix
- # This archive created: Tue Jan 5 00:50:17 1993
- # By: Ken McGlothlen
- export PATH; PATH=/bin:/usr/bin:$PATH
- echo shar: "extracting 'unix2dos'" '(131 characters)'
- if test -f 'unix2dos'
- then
- mv unix2dos unix2dos~; echo shar: "moved unix2dos to unix2dos~"
- fi; if test -d . ; then
- sed 's/^X//' << \OogaBooga > 'unix2dos'
- X#!/bin/sh
- X#
- X# Converts Unix text files to something more friendly to DOS, by adding
- X# CRs to the ends of each line.
- X
- Xsed 's/$/
- X
- OogaBooga
- if test 131 -ne "`wc -c < 'unix2dos'`"
- then
- echo shar: "error transmitting 'unix2dos'" '(should have been 131 characters)'
- fi
- chmod 700 'unix2dos'
- fi
- echo shar: "extracting 'dos2unix'" '(129 characters)'
- if test -f 'dos2unix'
- then
- mv dos2unix dos2unix~; echo shar: "moved dos2unix to dos2unix~"
- fi; if test -d . ; then
- sed 's/^X//' << \OogaBooga > 'dos2unix'
- X#!/bin/sh
- X#
- X# Converts a DOS text file to something more Unixy, by stripping the
- X# CRs off the ends of the lines.
- X
- Xsed 's/
- X
- OogaBooga
- if test 129 -ne "`wc -c < 'dos2unix'`"
- then
- echo shar: "error transmitting 'dos2unix'" '(should have been 129 characters)'
- fi
- chmod 700 'dos2unix'
- fi
- exit 0
- # End of shell archive
-