Padding file
對任意讀入的檔案進行 padding
程式應該沒甚麼特別的地方,逐步以 2 的倍數檢查 filesize,只是我多了一項檔案不能超過 2GB 的限制。
- Padding rule:以 1M, 2M, 4M ... 二的倍數去 padding
- 顯示補了多少 Bytes 的資料
- 操作方式:./padding <file>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
int fd;
struct stat filestat;
off_t alignmentsize;
if (argc != 2)
{
printf("padding <filename>\n");
return 0;
}
// open file
fd = open(argv[1], O_RDWR | O_EXCL);
if (fd < 0)
{
printf("%s\n", strerror(errno));
return 0;
}
// get filesize
fstat(fd, &filestat);
if (filestat.st_size >= (1ul << 31))
{
close(fd);
perror("File great than 2GB, it can not padding.");
return 0;
}
// check alignment size
alignmentsize = 1;
while (filestat.st_size > alignmentsize)
{
alignmentsize <<= 1;
}
printf("File %s is %ld bytes, need padding %ld bytes.\n", argv[1], filestat.st_size, (alignmentsize - filestat.st_size));
if (ftruncate(fd, alignmentsize) == -1)
{
printf("%s\n", strerror(errno));
}
close(fd);
return 0;
}
程式應該沒甚麼特別的地方,逐步以 2 的倍數檢查 filesize,只是我多了一項檔案不能超過 2GB 的限制。
留言