Mail Archives: djgpp/1996/11/22/21:45:23
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 -