C无法获取整个文件 [英] C fread not getting entire file

查看:45
本文介绍了C无法获取整个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取二进制文件并将内容存储到char数组中.此功能对于文本文件非常有用,但是对于非文本文件(例如PNG文件),则无法正常工作.下面是代码和结果.怎么了?

I am trying to read binary files and storing the content into a char array. This function is working great for text files, but for non-text files (a PNG file for example) it does not work as expected. Below is the code followed by the results. What is wrong?

代码:

#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!

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

I would expect these results if I opened the file in text mode, but since it is opening in binary mode, I do not see why this is happening.

推荐答案

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

The strlen function is exclusively for use on C-style strings. There is no way to tell the length of arbitrary binary data by looking at its content. You have the length in lengthRead.

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

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

Same problem here. The %s format specifier is for C-style strings, not arbitrary binary data. You'll need to write your own function to print the data in some appropriate format.

这篇关于C无法获取整个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆