home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / file / rwfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-07  |  1.0 KB  |  43 lines

  1. /* RWFILE.C: Reads and writes a file. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <sys\types.h>
  7. #include <sys\stat.h>
  8. #include <io.h>
  9.  
  10. #define BUFF 512
  11.  
  12. main()
  13. {
  14.    char inbuffer[BUFF];
  15.    char outbuffer[BUFF];
  16.    int infile, outfile, length, num;
  17.  
  18.    strcpy( outbuffer, "Happy Birthday." );
  19.    length = strlen( outbuffer );
  20.    length++;
  21.  
  22.    if( (outfile = open( "testfile.bin",
  23.       O_CREAT | O_WRONLY | O_BINARY,  S_IWRITE )) != -1 )
  24.    {
  25.       if( (num = write( outfile, outbuffer, length )) == -1 )
  26.          perror( "Error in writing" );
  27.       printf( "\nBytes written to file: %d\n", num );
  28.       close( outfile );
  29.    }
  30.    else
  31.       perror( "Error opening outfile" );
  32.  
  33.    if( (infile = open( "testfile.bin", O_RDONLY | O_BINARY )) != -1  )
  34.    {
  35.       while( length = read( infile, inbuffer, BUFF ) )
  36.          printf( "%d bytes received so far.\n", length );
  37.       close( infile );
  38.       printf( "%s\n", inbuffer );
  39.    }
  40.    else
  41.       perror( "Error opening infile" );
  42. }
  43.