Get a segment fault while reading a file

拟墨画扇 提交于 2019-12-06 09:39:34

The pointer you pass to fgets (file_content) is uninitialized. It should be pointing to a block of memory large enough to contain the specified number (fsize) of bytes. You can use malloc to allocate the memory.

char* file_content = (char*)malloc(fsize);

char * file_content is just a pointer, you need to allocate memory to store the string.

char * file_content;
file_content = malloc(fsize);

"..but I get a segment fault"

Obviously because you're attempting to write to an uninitialized file_content

Allocated memory for file_content before use

char * file_content =malloc(fsize);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!