在C中复制每个块的文件块

我正在尝试将文件划分为大小为y(以字节为单位)的x块,以便我可以单独复制每个块。 我怎样才能做到这一点?

尝试使用fread

 char buffer[ysize]; fread(buffer, ysize, 1, fp); 

每次从文件中读取缓冲区中的ysize字节。

一些struct stat结构中有其他成员,在复制文件时certificate是有用的:

      st_blksize文件的最佳I / O块大小。

      st_blocks为文件分配的实际块数
                  (查看当地系统)。

如果您读取的块大小是st_blksize的偶数倍,则您可以更有效地读取文件。

    size_t desiredSize = 1E4;  //要读入的最大缓冲区大小
    size_t blocks = desiredSize / st.st_blksize;
    if(blocks <1)//失败安全测试
        blocks = 1;
    size_t true_size = blocks * st.st_blksize;  //这是要阅读的大小
    char * buffer = malloc(true_size);

如果st_blksize失败,会为缓冲区大小提供BUFSIZ宏。

 x = fopen ( "x" , "rb"); if (x==NULL) {perror("file could not be opened"); exit(1);} y = fopen ( "y" , "wb"); if (x==NULL) {perror("file could not be opened"); exit(1);} char* buf = (char*) malloc (sizeof(char)*1024); //1024 is buffer size 

要将1024个字符读入缓冲区:

 fread(buf, sizeof(char), 1024, x); 

你做其余的事。