这个程序主要解决一些嵌入式系统没有 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.

67 lines
1.4 KiB

3 years ago
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <unistd.h>
  8. const char *get_packet_name(const char *filename, int num)
  9. {
  10. static char buf[256] = {0};
  11. sprintf(buf, "%s.%d", filename, num);
  12. return buf;
  13. }
  14. int main(int argc, char const *argv[])
  15. {
  16. //打开输入文件
  17. int fd = open(argv[1], O_RDONLY);
  18. if (fd < 0)
  19. {
  20. perror("openfail");
  21. exit(-1);
  22. }
  23. int fileindex = 1;
  24. uint32_t sumcheck = 0;
  25. uint8_t *data = malloc(100 * 1024 * 1024);
  26. if (data == NULL)
  27. {
  28. perror("malloc fail");
  29. exit(-1);
  30. }
  31. while (1)
  32. {
  33. int readlen = read(fd, data, 100 * 1024 * 1024);
  34. if (readlen <= 0)
  35. {
  36. printf("write end\n");
  37. break;
  38. }
  39. for (size_t i = 0; i < readlen; i++)
  40. {
  41. sumcheck += data[i];
  42. }
  43. //
  44. printf("create %s(%d)\n", get_packet_name(argv[1], fileindex), readlen);
  45. int tofd = open(get_packet_name(argv[1], fileindex), O_TRUNC | O_RDWR | O_CREAT,666);
  46. int writenum = write(tofd, data, readlen);
  47. if (writenum != readlen)
  48. {
  49. printf("Error: write fail,writenum!=readlen,%d != %d\n", writenum, readlen);
  50. exit(0);
  51. }
  52. close(tofd);
  53. fileindex++;
  54. }
  55. printf("sumcheck %d\n", sumcheck);
  56. return 0;
  57. }