|
发表于 2018-11-11 15:23:25
|
显示全部楼层
这很难吗?
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- int32_t GetFileSize(const char *filename)
- {
- FILE *file = fopen(filename, "r");
- if(!file)
- return -1;
- fseek(file, 0, SEEK_END);
- int32_t size = ftell(file);
- fclose(file);
- return size;
- }
- bool ReadFile(const char *filename, unsigned char *data, size_t size)
- {
- FILE *file = fopen(filename, "rb");
- if(!file)
- return false;
-
- fread(data, 1, size, file);
- fclose(file);
- return true;
- }
- bool WriteFile(const char *filename, const unsigned char *data, size_t size)
- {
- FILE *file = fopen(filename, "wb");
- if(!file)
- return false;
- fwrite(data, 1, size, file);
- fclose(file);
- return true;
- }
- int main(void)
- {
- int32_t size = GetFileSize("main.c");
- unsigned char *buf = malloc(size);
- printf("ReadFile: %d\n", ReadFile("main.c", buf, size));
- printf("WriteFile: %d\n", WriteFile("Copy.c", buf, size));
- fwrite(buf, 1, size, stdout);
- free(buf);
- return 0;
- }
复制代码 |
|