Important Notice
The pages on this site contain documentation for very old MS-DOS software,
purely for historical purposes.
If you're looking for up-to-date documentation, particularly for programming,
you should not rely on the information found here, as it will be woefully
out of date.
RWFILE.C
◄Up► ◄Contents► ◄Index► ◄Back►
────────────────────────────────────────────────────────────────────────────
/* RWFILE.C: Reads and writes a file. */
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys\types.h>
#include <sys\stat.h>
#include <io.h>
#define BUFF 512
main()
{
char inbuffer[BUFF];
char outbuffer[BUFF];
int infile, outfile, length, num;
strcpy( outbuffer, "Happy Birthday." );
length = strlen( outbuffer );
length++;
if( (outfile = open( "testfile.bin",
O_CREAT | O_WRONLY | O_BINARY, S_IWRITE )) != -1 )
{
if( (num = write( outfile, outbuffer, length )) == -1 )
perror( "Error in writing" );
printf( "\nBytes written to file: %d\n", num );
close( outfile );
}
else
perror( "Error opening outfile" );
if( (infile = open( "testfile.bin", O_RDONLY | O_BINARY )) != -1 )
{
while( length = read( infile, inbuffer, BUFF ) )
printf( "%d bytes received so far.\n", length );
close( infile );
printf( "%s\n", inbuffer );
}
else
perror( "Error opening infile" );
}