Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>read() read data from file</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 read()                  Read Data from File

 #include   <io.h>                       Required for declarations only

 int        read(handle,buffer,count);
 int           handle;                   Handle referring to open file
 char          *buffer;                  Data storage location
 unsigned int  count;                    Maximum number of bytes to read

    read() starts at the current position and reads 'count' bytes from
    the file associated with 'handle'. The bytes are stored in 'buffer'.
    On completion, the file pointer is positioned at the next unread
    character.

    Returns:    The number of bytes actually read, if successful; or zero
                if an attempt is made to read at end of file; or -1 if an
                error occurs.  On error, 'errno' is set to

                    EBADF:    'handle' is invalid; or file is not open
                              for reading (or file is locked, for MS-DOS
                              3.0 and later).

      Notes:    The maximum size for type 'int' is 32K.  If more than
                this amount is read from a file, the return value should
                be of type 'unsigned int'.  The maximum number of bytes
                that can be read is 65534, because 65535, or 0xFFFF,
                cannot be distinguished from -1, and will return an
                error.

                Under MS-DOS, the read terminates when the end-of-file
                marker (Ctrl-Z) is encountered in text mode.  Subsequent
                reads return 0. Close the file to clear the end-of-file
                indicator.

                The read function may return fewer than 'count' bytes, if
                there are less than 'count' bytes from the file pointer
                to the end of the file, or if the file was opened in text
                mode.  (In text mode, each CR-LF is counted as the single
                LF character it is translated to, thus the reason for the
                possible return of fewer than 'count' bytes. This
                translation does not affect the file pointer.)

 Portability:   Does not support ANSI standard.

   -------------------------------- Example ---------------------------------

    This example reads 100 bytes from an open file and prints out various
    messages depending on the results.

           #include <io.h>
           #include <stdio.h>
           #include <fcntl.h>

           char buffr[1000];

           main()
           {
                 int fhndl, ttlbytes;

                 if ((fhndl = open("info.dat",O_RDWR)) == -1) {
                     perror("error opening input file");
                     exit(1);
                  }
                  if ((ttlbytes = read(fhndl,buffr,100)) == -1)
                     perror("can't read from file");
                  else {
                       if (ttlbytes == 0)
                          printf("reached end of file");
                        else
                            printf("read %d bytes from file",ttlbytes);
                  }
            }


See Also: creat() fread() open() write()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson