[英]C-Segmentation fault (core dumped) error in linux
#include <stdio.h>
#include <stdlib.h>
struct fileIndex{
char name;
int key;
} index1;
int main(int argc, char *argv[]){
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
for(i=0; i<10; i++){
printf("%c",fgetc(pFile));
}
fclose(pFile);
}
When I want to run my program, it gives that error.当我想运行我的程序时,它给出了那个错误。 I looked so long for a wrong line in code, but I didn't find any.
我在代码中寻找错误的行很长时间,但我没有找到。 Can you help me ?
你能帮助我吗 ?
If the file failed to open, that will make pFile
equal NULL
, which can easily cause fgetc()
to segfault.如果文件无法打开,这将使
pFile
等于NULL
,这很容易导致fgetc()
出现段错误。
You must check for this before trying to read from the file:在尝试从文件中读取之前,您必须检查这一点:
if (pfile == NULL)
{
perror("Failed to open file");
exit(1);
}
change your code as将您的代码更改为
int i;
FILE *pFile;
pFile= fopen("cat/home/sysadmin/deneme.txt","r");
if(!pFile)
return;
Also.. looks like you file path is misplaced... are your meant t ouse /cat/home/sysadmin/deneme.txt
另外..看起来你的文件路径放错了位置......你的意思是要使用
/cat/home/sysadmin/deneme.txt
Is cat
your current directory or part of absolute path cat
是当前目录还是绝对路径的一部分
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.