www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/11/22/18:30:21

From: Erik Max Francis <max AT alcyone DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: read() question!!!!!!!!!
Date: Fri, 22 Nov 1996 09:52:33 -0800
Organization: Alcyone Systems
Lines: 23
Message-ID: <3295E861.2DC63245@alcyone.com>
References: <574j6m$ptc AT pulp DOT ucs DOT ualberta DOT ca>
NNTP-Posting-Host: newton.alcyone.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gorman Ho wrote:

> For some reason there is an extra 't' in the example. When I read a
> lot of characters (say 80 or so), there are 3 extra characters!! What
> am I doing wrong here??

Read just reads a block of data; in this case, 19 bytes worth.  You're then
passing this to puts, which expects a NUL-terminated string.  Your code
should instead look like this:

    char buffer[20] = read(file, buffer, 19);
    buffer[19] = 0; /* now it's NUL-terminated */
    puts(buffer);

Note you should also be checking the return value of the call to read to
check for errors.

-- 
                             Erik Max Francis | max AT alcyone DOT com
                              Alcyone Systems | http://www.alcyone.com/max/
                         San Jose, California | 37 20 07 N 121 53 38 W
                                 &tSftDotIotE | R^4: the 4th R is respect
         "But since when can wounded eyes see | If we weren't who we were"

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019