Path: utzoo!utgpu!water!watmath!clyde!rutgers!ucla-cs!cit-vax!elroy!ames!pasteur!agate!violet.berkeley.edu!dean From: dean@violet.berkeley.edu (Dean Pentcheff) Newsgroups: comp.sys.ibm.pc Subject: Re: Turbo C bug (?) Summary: This results from \r\n conversion Message-ID: <7056@agate.BERKELEY.EDU> Date: 19 Feb 88 06:55:39 GMT References: <734@agora.UUCP> Sender: usenet@agate.BERKELEY.EDU Reply-To: dean@violet.berkeley.edu.UUCP (Dean Pentcheff) Followup-To: comp.sys.ibm.pc Organization: University of California, Berkeley Department of Zoology Lines: 37 In article <734@agora.UUCP> qintar@agora.UUCP (Jim Seymour) writes: >In version 1.0 I noticed what seemed to be a bug in the read() function. >The docs claim (and the standard dictates) that this function returns the >actual byte count read in from the file... Actually, the reference under read() claims that "... a positive integer is returned indicating the number of bytes placed in the buffer; if the file was opened in text mode, read does not count carriage returns or Ctrl-Z characters in the number of bytes read." A little experimentation clarifies this somewhat ambiguous statement. Read() (in text mode) will read the requested number of bytes from the file, but _then_ perform \r and ^Z elimination, fill your buffer with whatever remains, and return the final number of characters that ended up in your buffer (_after_ \r and ^Z removal). A suggested fix is to use the Turbo _read() function which operates purely, without \r and ^Z manipulation. Alternately, you could open the file using the O_BINARY flag, and use the normal read(). One interesting "feature" connected with this is the ambiguous case of reading a file (in text mode) which consists of only '\r' characters. The result turns out to be rather ugly: if you read a buffer's worth of '\r's, Turbo can't return a 0 for the number of characters placed in the buffer, since that is supposed to mean EOF. So, read() returns a 1, and (usually!) leaves a '\r' as the first character in the buffer. On the last read before EOF, though, it seems to leave garbage in that character position. Gag, ack, blech. -Dean ----------------- Dean Pentcheff (dean@violet.berkeley.edu) ----------------- "A university is a place where people pay high prices for goods which they then proceed to leave on the counter when they go out of the store." Loren Eiseley