The below program giving the following error when compiling:
./vpl_test: line 2: 18699 Segmentation fault (core dumped) ./solution
What could be the problem with the below C program ?
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void sort(long *sorted, int count,long value){
int i=0;
sorted[count] = value;
if(count == 0)return;
for(i=count;i>=0;i--){
if(value<sorted[i-1])
sorted[i] = sorted[i-1];
else break;
}
sorted[i]=value;
}
int main(int argc,char *argv[]){
FILE *fp = NULL;
long sorted[1024];
long value;
int count = 0;
int i=0;
fp = fopen("brandlist.txt","r+");
//fp = fopen("brandlist.txt","w");
if(NULL == fp){
perror("fopen");
exit(0);
}
while(!feof(fp)){
fscanf(fp,"%ld\n",&sorted[i]);
sort(sorted,count,value);
++count;
}
for(i=0;i<count;i++){
fprintf(fp,"%ld\n",sorted[i]);
}
if(fp){
fclose(fp);
fp = NULL;
}
}