[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference decwet::nt-developers

Title:MS Windows NT Developers
Notice:See note 1222 for MS bug reporting info
Moderator:TARKIN::LINEIBER
Created:Mon Nov 11 1991
Last Modified:Tue Jun 03 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3247
Total number of notes:15633

3219.0. "C library read() anomaly?" by TUXEDO::ZEE (There you go.) Tue Apr 08 1997 21:26

I'm running NT 4.00.1381, and one of our database utility programs is not
getting very far with fread() nor read().  We've narrowed the behavior down
to the following test program, which just reads in 512 bytes at a time
and then writes them out to a second file.  The test file is
over 200K bytes, but the test program below only reads in 4900 bytes
before the _read() returns 0.  Are there any system variables or other
knobs that might explain this unexpected behavior?  I'm new to NT (from Unix),
and the Unix version works as expected.  FYI, you can try it yourself -
the test file is available via anonymous ftp at jfcl.lkg.dec.com
(16.20.16.101) in tmp/checkpt.data.

--Roger

---- test program below ----
/* read_test.c: This program reads in 512 bytes at a time
 * and writes them out to a second file.
 *
 * Usage:  read_test file1 file2
 */

#include <fcntl.h>      /* Needed only for O_RDWR definition */
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

void main (
	   int argc,
	   char *argv[] )
{
    int   fd_r, fd_w, readcount, byteswritten;
    int   total_read = 0, total_written = 0;
    char  buffer[512];

    /* Open file for input: */
    if ( (fd_r = open( argv[1], O_RDONLY )) == -1 ) {
	perror( "open failed on input file" );
	exit( 1 );
    }

    /* Open file for output: */
    if ( (fd_w = open( argv[2], O_RDWR | O_CREAT,
                      S_IREAD | S_IWRITE)) == -1 ) {
	perror( "open failed on input write file" );
	exit( 1 );
    }

    /* Read in input: */
    /* Cycle until end of file reached: */
    while ((readcount = read( fd_r, buffer, 512 )) > 0 ) {

	/* Total actual bytes read */
	total_read += readcount;
	printf("read %d bytes \n", readcount);

	if (( byteswritten = write( fd_w, buffer, readcount)) == -1 ) {
	    perror( "Write failed" );
	}
	else {
	    printf( "Wrote %u bytes to file\n", byteswritten );
	    total_written += byteswritten;
	}
    } /* end while */

    printf("errno = %d\n", errno);
    perror("After read: ");
    printf( "Number of bytes read = %d, written = %d\n",
	   total_read, total_written );
    close( fd_r );
    close( fd_w );
}
T.RTitleUserPersonal
Name
DateLines
3219.1Binary files ??? or ASCII ??CANDOO::GRIEBWed Apr 09 1997 12:304
Depending on what you are trying to do it might work better by
adding in O_BINARY to the open statments.

3219.2use O_BINARY on open()TUXEDO::ZEEThere you go.Wed Apr 09 1997 12:583
Some kind soul pointed out my problem.

--Roger