Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当我提交代码时,我发现codechef中存在SIGSEGV运行时错误_C_Segmentation Fault - Fatal编程技术网

当我提交代码时,我发现codechef中存在SIGSEGV运行时错误

当我提交代码时,我发现codechef中存在SIGSEGV运行时错误,c,segmentation-fault,C,Segmentation Fault,这是我的代码 #include<stdio.h> long int a[100001],b[100001]; int main() { long int n=0,m=0,bp=0,bn=0,i=0,x=0,Result=0;//declaration char ch='\0'; FILE *fp; scanf("%ld %ld",&n,&m); fp=fopen("array.txt","w");//file reading for(i=0;i<=n;i++)

这是我的代码

#include<stdio.h>
long int a[100001],b[100001];
int main()
{
long int n=0,m=0,bp=0,bn=0,i=0,x=0,Result=0;//declaration
char ch='\0';
FILE *fp;
scanf("%ld %ld",&n,&m);
fp=fopen("array.txt","w");//file reading
for(i=0;i<=n;i++)
{
    ch=getchar();
    fputc(ch,fp);
}
fclose(fp);
fp=fopen("array.txt","r");//file scaning to array
for(i=0;i<n;i++)
{
    fscanf(fp,"%1d",&a[i]);
}
fclose(fp);
while(m>0)
{
    scanf("%ld",&x);
    bp=0;
    bn=0;
    for(i=0;i<x-1;i++)
    {
        b[i]=a[x-1]-a[i];
        if(b[i]>0)
            bp = bp+b[i];
        else
            bn = bn+b[i];
    }
    Result=bp-bn;
    printf("%ld\n",Result);
    m--;
}
return (0);
}
#包括
长整数a[100001],b[100001];
int main()
{
long int n=0,m=0,bp=0,bn=0,i=0,x=0,Result=0;//声明
char ch='\0';
文件*fp;
scanf(“%ld%ld”,&n,&m);
fp=fopen(“array.txt”,“w”);//文件读取

对于(i=0;i您不允许在Codechef上打开文件进行读/写。这就是您获得SIGSEGV的原因

我不明白有必要对该问题执行文件I/O


此外,您应该删除代码,因为这是运行竞赛时遇到的问题。

您对fopen的调用没有错误检查-您如何知道文件已成功创建?当您打开文件进行写入时,为什么评论会说“文件读取”?误导性评论比没有评论更糟糕。