home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc / qc25 / rwfile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-15  |  1.0 KB  |  42 lines

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