0
這是我的代碼IAM被陷在codechef SIGSEGV運行時錯誤,當我提出我的代碼
#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);
}
,當我在我的系統運行這段代碼它給正確的輸出,但在codechef它顯示SIGSEGV運行時錯誤...請幫我寫一個高效的程序 linkto問題是http://www.codechef.com/APRIL14/problems/ADIGIT
你沒有錯誤檢查fopen的調用 - 你怎麼知道文件被成功創建? –
爲什麼當您打開文件進行寫作時,評論會說「文件閱讀」?誤導評論比沒有評論更糟糕。 – tripleee