Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ fread(DOS) — OpenDesktop Software Development System 1.0.0d

Media Vault

Software Library

Restoration Projects

Artifacts Sought

     Name
          fread - Reads from the input stream.

     Syntax
          #include <stdio.h>

          size_t fread(buffer, size, count, stream)
          void *buffer;
          size_t size;
          size_t count;
          FILE *stream;

     Description
          The fread function reads up to count items of size bytes
          from the input stream and stores them in buffer. The file
          pointer associated with stream (if there is one) is
          increased by the number of bytes actually read.

          If the given stream is opened in text mode, carriage-
          return-line-feed (CR-LF) pairs are replaced with single
          line-feed (LF) characters. The replacement has no effect on
          the file pointer or the return value.

          The file-pointer position is indeterminate if an error
          occurs. The value of a partially read item cannot be
          determined.

     Return Value
          The fread function returns the number of full items actually
          read, which may be less than count if an error occurs or if
          the file end is encountered before reaching count.

          The feof or ferror function should be used to distinguish a
          read error from an end-of-file condition. If size or count
          is 0, fread returns 0 and the buffer contents are unchanged.

     See Also
          fwrite(DOS), read(DOS)

     Example
          #include <stdio.h>

          FILE *stream; long list[100]; int numread; int numwritten;

          main()
           {

                  /* Open file in "binary" mode: */
                  if ((stream = fopen("data", "w+b")) != NULL)
                  {
                          /* Write 100 long integers to "stream": */
                          numwritten = fwrite((char
          *)list,sizeof(long),100,stream);
                          printf( "Wrote %d items\n", numwritten );
                  }
                  else
                          printf("Problem opening the file\n");
                  fclose(stream);

                  if ((stream = fopen("data", "r+b")) != NULL)
                  {
                          /* Attempt to read in 100 long ints: */
                          numread = fread((char *)list,
                                     sizeof(long),100,stream);
                          printf("Number of items read = %d\n",
          numread);
                  }
                  else
                          printf("Was not able to open the file\n");
           }

          This program opens a file named data.bin and writes 100 long
          integers to the file. It then tries to open data.bin and
          read in 100 long integers. If the attempt succeeds, the
          program displays the number of actual items read.

                                                (printed 6/18/89)



Typewritten Software • bear@typewritten.org • Edmonds, WA 98026