简体   繁体   中英

How to use read and write past BUFSIZ in C

For an assignment, I'm supposed to create two methods: Method one will read() and write() the input file to an empty output file, one byte at a time (slowly).

The other method will instead use char buf[BUFSIZ]; where BUFSIZ is from <stdio.h> . We are supposed to read() and write() with the BUFSIZ which will make things a lot faster.

The input file we test each method on is just a linux dictionary ( /dict/linux.words ).

I've correctly implemented method one, where I call read() and write() on one character at a time, copying the input file to the output file. Although it's very slow, it at least copies everything over.

My code for this looks like this:

// assume we have a valid, opened fd_in and fd_out file.
char buf;
while(read(fd_in, buf, 1) != 0)
    write(fd_out, buf, 1);

For method two however, where I use BUFSIZ , I am not able to transfer every single entry into the output file. It fails in the z entries, and doesn't write anymore.

So, my first try:

// assume we have a valid, opened fd_in and fd_out file
char buf[BUFSIZ];
while(read(fd_in, buf, BUFSIZ) != 0)
    write(fd_out, buf, BUFSIZ);

doesn't work.

I understand that read() will return either the number of bytes read or 0 if it is at the end of a file. The problem I'm having is understanding how I can compare read() to BUFSIZ , and then loop around and start read() at where it left off until I reach the real end of file.

Since your file will most likely not be an exact multiple of BUFSIZ you need to check for the actual number of bytes read, so that the last block will be written correctly, eg

char buf[BUFSIZ];
ssize_t n;
while((n = read(fd_in, buf, BUFSIZ)) > 0)
    write(fd_out, buf, n);
this code:

// assume we have a valid, opened fd_in and fd_out file
char buf[BUFSIZ];
while(read(fd_in, buf, BUFSIZ) != 0)
    write(fd_out, buf, BUFSIZ);

leaves much to be desired, 
does not handle a short remaining char count at the end of the file, 
does not handle errors, etc.

a much better code block would be:

// assume we have a valid, opened fd_in and fd_out file
char buf[BUFSIZ];
int readCount;  // number of bytes read
int writeCount; // number of bytes written

while(1)
{
    if( 0 > (readCount = read(fd_in, buf, BUFSIZ) ) )
    { // then, read failed
         perror( "read failed" );
         exit( EXIT_FAILURE );
    }

    // implied else, read successful

    if( 0 == readCount )
    {  // then assume end of file
        break; // exit while loop
    }

    // implied else, readCount > 0

    if( readCount != (writeCount = write( fd_out, buf, readCount ) ) )
    { // then, error occurred
        perror( "write failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, write successful
} // end while

Note: I did not include the closing of input/output files statements before each call to exit() however, that does need to be added

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM