Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Borland C++ 2.x ( with Turbo C ) - <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
 void       *buffer;                     Data storage location
 unsigned   count;                       Maximum number of bytes to read

    read() reads 'count' bytes from an open file or device specified by
    'handle' and stores the bytes read in 'buffer'.  If the handle is
    associated with a disk file, reading starts at the current position
    of the file pointer; after the read, the file pointer points to the
    next unread character. If the handle is associated with a device,
    read() reads directly from it.

    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).
                    EACCES:   Permission denied

         Notes:     If you are reading more than 32K (the maximum size
                    for type 'int') from a file, the return value should
                    be of type 'unsigned int'.  The maximum number of
                    bytes that can be read is 65534. (65535, or 0XFFFF,
                    is indistinguishable from -1, and will return an
                    error.)

                    Under MS-DOS, when the end-of-file marker is
                    encountered in text mode (Ctrl-Z), the read
                    terminates, and subsequent reads return 0. The file
                    must be closed 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. This translation does not affect the file
                    pointer.)

   -------------------------------- 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