这个程序主要解决一些嵌入式系统没有 split指令,无法对大文件进行分离打包
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
const char *get_packet_name(const char *filename, int num) { static char buf[256] = {0}; sprintf(buf, "%s.%d", filename, num); return buf; }
int main(int argc, char const *argv[]) {
//打开输入文件
int fd = open(argv[1], O_RDONLY); if (fd < 0) { perror("openfail"); exit(-1); }
int fileindex = 1; uint32_t sumcheck = 0; uint8_t *data = malloc(100 * 1024 * 1024); if (data == NULL) { perror("malloc fail"); exit(-1); }
while (1) {
int readlen = read(fd, data, 100 * 1024 * 1024); if (readlen <= 0) { printf("write end\n"); break; }
for (size_t i = 0; i < readlen; i++) { sumcheck += data[i]; }
//
printf("create %s(%d)\n", get_packet_name(argv[1], fileindex), readlen); int tofd = open(get_packet_name(argv[1], fileindex), O_TRUNC | O_RDWR | O_CREAT,666); int writenum = write(tofd, data, readlen); if (writenum != readlen) { printf("Error: write fail,writenum!=readlen,%d != %d\n", writenum, readlen); exit(0); } close(tofd); fileindex++; } printf("sumcheck %d\n", sumcheck);
return 0; }
|