gpt4 book ai didi

C fread 没有得到整个文件

转载 作者:太空宇宙 更新时间:2023-11-03 23:32:04 25 4
gpt4 key购买 nike

我正在尝试读取二进制文件并将内容存储到一个字符数组中。此函数对于文本文件非常有效,但对于非文本文件(例如 PNG 文件)它无法按预期工作。下面是代码和结果。怎么了?

代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <fcntl.h>

unsigned int readFile(const char *fileName, char **contents);

int main(int argc, char *argv[])
{
char *contents = 0;
unsigned int length = readFile(argv[1], &contents);

fprintf(stderr, "File length: %d\r\n", length);
fprintf(stderr, "File contents:\r\n%s\r\n", contents);

exit(0);
}

unsigned int readFile(const char *fileName, char **contents)
{
struct stat stbuf;
unsigned int length = 0;

// Open a file descriptor
int fd = open(fileName, O_RDONLY);

// Check that file descriptor was opened
if (fd == -1) {
fprintf(stderr, "Fatal Error: Failed to open the file for fstat (file: %s)!\r\n", fileName);
exit(-1);
}

// Get information from fstat
if (fstat(fd, &stbuf) == -1) {
fprintf(stderr, "Fatal Error: Failed to find file length (file: %s)!\r\n", fileName);
exit(-1);
}

// Store the file length
length = stbuf.st_size;

// Close file descriptor
if (close(fd) == -1) {
fprintf(stderr, "Fatal Error: Failed to close file descriptor (file: %s)!\r\n", fileName);
exit(-1);
}

// Check if the file contains data
if (length > 0) {
// Open the file for reading
FILE *file = fopen(fileName, "rb");

// Check that the file was opened
if (file != NULL) {
freopen(fileName, "rb", file);
// Prepare the contents variable
*contents = 0;
*contents = (char*)calloc(length + 1, sizeof(char));

// Read the file and put it in the contents variable
int lengthRead = fread(*contents, length, 1, file);

// Check for file read error
if (ferror(file)) {
fprintf(stderr, "Fatal Error: Failed to read file (file: %s)!\r\n", fileName);
exit(-1);
}
else if (lengthRead != 1 || strlen(*contents) < length) {
fprintf(stderr, "Fatal Error: File read error (file: %s)!\r\n", fileName);
fprintf(stderr, "Characters expected: %d, Characters read: %d\r\n", length, strlen(*contents));
fprintf(stderr, "Content read:\r\n%s\r\n", *contents);
fprintf(stderr, "Aborting!\r\n", fileName);

// Close file and exit
fclose(file);
exit(-1);
}

// Close binary file
fclose(file);
}
else {
fprintf(stderr, "Fatal Error: Failed to open the file: %s!\r\n", fileName);
exit(-1);
}
}
else {
fprintf(stderr, "Fatal Error: File was empty (file: %s)!\r\n", fileName);
exit(-1);
}

return length;
}

结果:

devious@devious-kubuntu:~/Workspace/test$ ls                                                                                                                                                                                                                                   
test test.c test.png test.txt
devious@devious-kubuntu:~/Workspace/test$ ./test test.txt
File length: 43
File contents:
This is a test file!

With multiple lines!

devious@devious-kubuntu:~/Workspace/test$ ./test test.png
Fatal Error: File read error (file: test.png)!
Characters expected: 50243, Characters read: 8
Content read:
?PNG
¦

Aborting!

如果我以文本模式打开文件,我希望得到这些结果,但由于它是以二进制模式打开的,所以我不明白为什么会这样。

最佳答案

strlen 函数专门用于 C 风格的字符串。无法通过查看其内容来判断任意二进制数据的长度。您在 lengthRead 中有长度。

            fprintf(stderr, "Content read:\r\n%s\r\n", *contents);

同样的问题。 %s 格式说明符用于 C 风格的字符串,而不是任意二进制数据。您需要编写自己的函数以某种适当的格式打印数据。

关于C fread 没有得到整个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13223666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com