从未定义长度的文件中读取,存储在数组中,出现分段错误 [英] Reading from file of undefined length, storing in array, segmentation fault

查看:116
本文介绍了从未定义长度的文件中读取,存储在数组中,出现分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开一个文件,读取其内容,然后使用C代码将其存储在数组中.

I want to open a file, read its content and store it in an array using C code.

我在Windows笔记本电脑上做到了,它可以工作,但是当我在Raspberrypi上尝试代码时,出现了分段错误.我已经尝试了一段时间来调试我对C还是很陌生,所以我很难找到我做错了什么.

I did it on my Windows laptop and it works but when i try the code on my Raspberrypi i get segmentation faults. I've been trying for a while to debugm I'm quite new to C so I'm having trouble finding what I did wrong.

    char *readFile(char *fileName)
    {
        FILE *ptr_file;
        char *ptr_data;
        int n = 0;
        char c;

        ptr_file = fopen(fileName, "r");
        if(ptr_file == NULL)
        {
            perror("File could not be opened.\n");
            exit(EXIT_FAILURE);
        }
        fseek(ptr_file, 0, SEEK_END);
        long f_size = ftell(ptr_file);
        fseek(ptr_file,0, SEEK_SET);
        ptr_data = (char *)malloc(f_size+1);

if(ptr_data == NULL)
{
perror("MALLOC FAILED");
exit(EXIT_FAILURE);
}

        while((c = fgetc(ptr_file)) != EOF)
        {
                ptr_data[n++] = (char)c;

        }
        ptr_data[n] = '\0';
        fclose(ptr_file);
        return ptr_data;
    }

在我看来,分段错误似乎是在调用malloc之后的while循环中出现的.

to me it seems like the segmentations fault appears in the while loop after the call to malloc.

为什么它可以在我的笔记本电脑上而不是raspberrypi上运行?

Why does it work on my laptop and not on the raspberrypi?

同时,我不理解为什么如果我这样做,为什么我的RPi上出现分段错误:

at the same time i dont understand why i get segmentation faults on my RPi if id do this:

   int main(int argc, char *argv[])
            {
    char data[100] = {};
                FILE *ptr_file;
                char *ptr_data=data;
                int n = 0, i = 0;
                char c;

                ptr_file = fopen(fileName, "r");
                if(ptr_file == NULL)
                {
                    perror("File could not be opened.\n");
                    exit(EXIT_FAILURE);
                }

                while((c = fgetc(ptr_file)) != EOF)
                {
                        ptr_data[n++] = (char)c;

                }
                ptr_data[n] = '\0';
                while(i <n,i++)
    {
    printf("%c\n",data[i]);
    fclose(ptr_file);

        }

返回0; }

推荐答案

在不同环境下读取文本文件时存在一些问题. 例如,编写新行时,在Windows上可能会消耗2个字节,而在Linux上可能仅消耗1个字节. 摘自文章:

There are some issues when reading text file on different environment. When writing a new line, for example, may consume 2 bytes on Windows, and just 1 on Linux. From an article:

C标准[ISO/IEC 9899:2011]的7.21.9.4节规定了 在文本模式下打开文本文件时,ftell()的以下行为: For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call.因此,流的ftell()返回值 在文本模式下打开的模板绝不能用于其他偏移量计算 比对fseek()的调用要多.

Subclause 7.21.9.4 of the C Standard [ISO/IEC 9899:2011] specifies the following behavior for ftell() when opening a text file in text mode: For a text stream, its file position indicator contains unspecified information, usable by the fseek function for returning the file position indicator for the stream to its position at the time of the ftell call. Consequently, the return value of ftell() for streams opened in text mode should never be used for offset calculations other than in calls to fseek().

换句话说,fseek和ftell函数的行为可能会有所不同,具体取决于您所使用的环境.
有关进一步的说明,您可以阅读以下主题: https://www.securecoding.cert.org/confluence/display/seccode/FIO14-C.+理解++文本+模式+和+二进制+模式+之间的差异+文件+流

In another words, fseek and ftell function behavior may be different depending on which environment you're working with.
For further explanation you may read this topic: https://www.securecoding.cert.org/confluence/display/seccode/FIO14-C.+Understand+the+difference+between+text+mode+and+binary+mode+with+file+streams

这篇关于从未定义长度的文件中读取,存储在数组中,出现分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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