Ç - 读文件和分割故障 [英] C - reading a file and segmentation fault

查看:183
本文介绍了Ç - 读文件和分割故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C(日食在linux下)的程序,所以我需要打开一个大的文本文件,并读取它(比尝试每次缓冲区的大小不同势)

Im writing a program in C (eclipse in linux) so I need to open a big text file and read it (and than try with diffrent size of buffer each time)

不管怎么说,这是code,我不明白为什么我可是从打开的功能越来越分段故障

Anyways, this is the code and I don't understand why Im getting segmentation fault from the open function

int main(void)
{
    int fd;
    char* buff[67108864];
    FILE *testfile;
    double dif;
    fd = open("testfile.txt", O_RDONLY);
    if (fd>=0) {
        read(fd,buff,67108864);
        close(fd);      }

    return 0;
}

我已经编辑我的问题,但现在如果我改变缓冲区的最大尺寸,我需要(67108864字节)IM仍然得到段错误...

I have edited my question but now if I change my buffer to the biggest size I need (67108864 bytes) im still getting segmentation fault...

推荐答案

如果您想让内存分配到的buff,你需要使它的指针。

well if you want to allocate memory into buff, you need to make it a pointer..

char* buff;

也注意到你只分配一个字符..你应该考虑的是,我想你想使用更多的内存。

also notice you only allocate one char.. you should consider that, I think you want to use more memory..

另一个共同点是不使用的动态内存的文件读取。

another common thing is not using dynamic memory for file reading..

尝试:

char buff[100]; 

然后是一样的code ...

and then just the same code...

read(fd,buff,100));

,然后就继续读,直到查找完毕后,read返回的字节数实际读取。

and then just keep reading until the find is complete, read returns the amount of bytes actually read.

另外像上面的评论,你正在使用testfile的初始化它..这也是一个访问冲突之前

Also like commented above, you are using testfile before initializing it.. that is also an access violation

这篇关于Ç - 读文件和分割故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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