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

From: elric AT wheel DOT dcn DOT davis DOT ca DOT us (Jeffrey Taylor)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: read() question!!!!!!!!!
Date: 22 Nov 1996 19:31:41 GMT
Organization: Davis Community Network - Davis, California, USA
Lines: 34
Message-ID: <574v2t$hr2@mark.ucdavis.edu>
References: <574j6m$ptc AT pulp DOT ucs DOT ualberta DOT ca>
NNTP-Posting-Host: wheel.dcn.davis.ca.us
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gorman Ho (gorman AT gpu DOT srv DOT ualberta DOT ca) wrote:
: I have a slight problem with libc.a's read() function. When I execute
: the following code:
: 
: ---
: #include <all header files required>
: 
: main()
: {
:   int File = open(Filename, O_RDONLY);
:   char Buffer[20] = read(File, Buffer, 19);

This line reads 19 bytes into Buffer and writes the return value (the
number of bytes read, probably 19) into Buffer[0].

:   puts(Buffer);

This line writes from the start of Buffer up to the first NUL byte ('\0')
to the standard output.  Unless what you read from the file had a NUL byte
in it, this may go on for at while.  Also that 19 in the first byte is an
non-printing character and will do funny things dependent on your exact
setup.

:   close(File);
:   return 0;
: }
: --
: Better is :
   char Buffer[20];
   Buffer[read(file, Buffer, 19)] = '\0';
   puts(Buffer);

Hope this helps

- Raw text -


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