简体   繁体   中英

How do I properly use fread() and fwrite() in my program?

/*Write a program that indefinitely takes in files to append to a master
  file. If the file names are the same, don't do it. If there's an error,
  don't do it. Exit with a blank line. Use a custom buffer.*/ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 25
#define BUFSIZE 1024
int append(FILE *, FILE *); 

int main(){
    char src[MAX_NAME], mainfile[MAX_NAME]; 
    FILE *src_ptr, *mainfile_ptr; 
    int successes = 0; 

    puts("Enter name of main file"); 
    if(gets(mainfile) == NULL){
        fprintf(stderr, "Error, couldn't get filename\n"); 
        return 1; 
    }
    if((mainfile_ptr = fopen(mainfile, "ab")) == NULL){
        fprintf(stderr, "Error, couldn't open mainfile\n");
        return 1; 
    }

    do{
        puts("Enter a filename to be appended"); 
        if(gets(src) == NULL || src[0] == '\0' || strcmp(src, mainfile) == 0){
            if(src[0])
                puts("Error, couldn't get filename!"); 
            continue; 
        } 

        if((src_ptr =fopen(src, "rb")) == NULL){
            puts("Error, could not open file");
            continue; 
        }

        if(setvbuf(src_ptr, NULL, _IOFBF, BUFSIZE) != 0){
            puts("Couldn't create filebuffer!"); 
            continue; 
        }

        if(append(src_ptr, mainfile_ptr) != 0){
            fflush(src_ptr); 
            puts("Couldn't append!"); 
            continue; 
        }

        fclose(src_ptr); 
        ++successes; 
        printf("Successfully appended %s, %d files total\n", src, successes);

    }while(src[0] != '\0'); 

    fclose(mainfile_ptr); 
    printf("Successfully appended %d files, bye!\n", successes); 
    return 0; 
}


int append(FILE *src, FILE *mainfile){
    size_t bytes = 0; 
    static char buffer[BUFSIZE];
    while(bytes = (fread(buffer, sizeof(char), BUFSIZE, src)) > 0)
       fwrite(buffer, sizeof(char), BUFSIZE, mainfile);  

    if(ferror(src) || ferror(mainfile))
        return 1; 

    return 0; 
}

Hi there. Per a C Primer Plus example, I wrote a program that would append the contents of user-specified files to a master file. Initially I wrote it intending only to use it with text files, but then I changed it to work in binary mode. I used it to append four files. I got some really weird results and I don't know why. Here's a screenshot of the finished file (I had to screenshot it because it's full of really weird characters). I get the same results without opening them in binary mode with fopen. It seems to "append" my files multiple times, some of which only half way and then later the other half.

Where did I go wrong?

在gvim中查看文件

I don't know why it resized it so much.

while(bytes = (fread(buffer, sizeof(char), BUFSIZE, src)) > 0)
    fwrite(buffer, sizeof(char), BUFSIZE, mainfile); 

should be

while((bytes = fread(buffer, sizeof(char), BUFSIZE, src)) > 0)
    fwrite(buffer, sizeof(char), bytes, mainfile);

In the cases where fread returns less than BUFSIZE bytes, you're writing data from previous reads into mainfile .

You could also check that fwrite returns bytes , choosing whether to abort or retry writing the remainder of the buffer if less is written.

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