FILE 流缓冲区如何工作?

How does FILE stream buffer work?

我了解 fopen() 打开文件并为该文件的读写操作创建缓冲区。 fopen() returns 该缓冲区的指针。

所以我的问题是,在下面的代码中,_copy 函数体有一个 temp 矩阵在 fread()fwrite() 之间传递。为什么我不能直接从缓冲区传输到缓冲区?

/* example: copyfile.exe xxxxx.txt   zzzzzz.txt */
#include <stdio.h>
#include <stdlib.h>

#define BUFF 8192
void _copy(FILE *source, FILE *destination);

int main(int argc, char *argv[])
{
    FILE *fp1, *fp2;  // fp1 source file pointer// fp2 copied file pointer 

    if (argc !=3 )           //command line must have 3 arguments
    {
        fprintf(stderr, "Usage: %s (source file) (copy file)\n", argv[0][0]);
        exit(EXIT_FAILURE);
    }

    if ((fp1 = fopen(argv[1], "rb")) == NULL)    //Opening source file
    {
        fprintf(stderr, "Could not open %s\n",argv[1]);
        exit(EXIT_FAILURE);
    }
    if((fp2 = fopen(argv[2], "ab+")) == NULL)  //Opening destination file
    {
        fprintf(stderr, "could not create %s \n",argv[2]);
        exit(EXIT_FAILURE);
    }
    if( setvbuf(fp1,NULL, _IOFBF, BUFF) != 0) //Setting buffer for source file
    {
        fputs("Can't create output buffer\n", stderr);
        exit(EXIT_FAILURE);
    }
    if( setvbuf(fp2,NULL, _IOFBF, BUFF) != 0) //Setting buffer for destination file
    {
        fputs("Can't create input buffer\n", stderr);
        exit(EXIT_FAILURE);
    }


    _copy(fp1, fp2);  
    if (ferror(fp1)!=0)
        fprintf(stderr, "Error reading file %s\n", argv[1]);
    if(ferror(fp2)!=0)
        fprintf(stderr, "Error writing file %s\n",argv[2]);
    printf("Done coping %s (source) to %s (destination) \n",argv[1], argv[2]);
    fclose(fp1);
    fclose(fp2);
    return (0);
}

void _copy(FILE  *source, FILE *destination)
{
    size_t bytes;
    static char temp[BUFF];

    while((bytes = fread(temp,sizeof(char),BUFF,source))>0)
        fwrite(temp,sizeof(char),bytes,destination);
}

您不能在另一个 FILE * 中使用来自 FILE * 的底层缓冲区。正如您在评论中所说, FILE * 是一个不透明的指针。但是您可以通过强制两个文件都处于非缓冲模式来避免在缓冲区之间复制数据的开销:

setbuf(fp, NULL);   // cause the stream to be unbuffered