我正在尝试将文件划分为x个大小为y的块(以字节为单位),以便我可以单独复制每个块。我怎么能这样做?
答案 0 :(得分:4)
答案 1 :(得分:1)
一些struct stat结构中有其他成员,在复制文件时证明是有用的:
st_blksize The optimal I/O block size for the file. st_blocks The actual number of blocks allocated for the file in (check local system).
如果您读取的块大小是st_blksize的偶数倍,则您可以更有效地读取文件。
size_t desiredSize = 1E4; // largest buffer size to read into size_t blocks = desiredSize / st.st_blksize; if ( blocks < 1 ) // fail safe test blocks = 1; size_t true_size = blocks * st.st_blksize; // this is the size to read char *buffer = malloc(true_size);
st_blksize失败,&lt; stdio.h&gt;为缓冲区大小提供BUFSIZ宏。
答案 2 :(得分:0)
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);
你做其余的事。