#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#define BUFFER_SIZE (4096*2)
void print_file_content(const char *filename) {
int fd = open(filename, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "Error opening file '%s': %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
char buffer[BUFFER_SIZE];
ssize_t bytes_read;
while ((bytes_read = read(fd, buffer, BUFFER_SIZE)) > 0) {
for (ssize_t i = 0; i < bytes_read; i++) {
// 将读取到的内容打印出来,空洞部分会打印为零
// putchar(buffer[i] ? buffer[i] : '0');
printf("%x",buffer[i]);
}
}
if (bytes_read == -1) {
fprintf(stderr, "Error reading file '%s': %s\n", filename, strerror(errno));
close(fd);
exit(EXIT_FAILURE);
}
close(fd);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
exit(EXIT_FAILURE);
}
print_file_content("./test.txt");
return 0;
}
其中test.txt为空洞文件。前4k为空洞,后4k为之前写进去的ff