Hello I am getting error as segmentation fault when my file contains large number of line(approx. 44000. However if I run the same program with lesser number of lines, it works fine. I am not getting the reason for this error and how to rectify it.
int main()
{
FILE *fp,*file,*f1;
int **phone;
int count = 0;
f1 = fopen("phones.txt","r"); //open files which has phones
int noOfLines = count_lines(); // get no of lines in phone.txt
//printf("%d\n",noOfLines);
//int phone[30][noOfLines]={0}; // variable-sized object may not be initialized
phone = (int**) calloc(30,sizeof(int*)); // create array using malloc
int i;
for (i=0;i<30;i++)
phone[i] = (int*) calloc(noOfLines,sizeof(int));
char line2[128];
if ( f1 != NULL ) // check if the files exist
{
while(fgets(line2, sizeof(line2), f1) != NULL) // get each line from file
{
line2[strlen(line2)-1]='\0'; // to remove the last \n
//printf("%s\t",line2);
file = fopen("list.txt","r"); // open file which has the list of .phseg -->this is the large file
if ( file != NULL)
{
char line1[128];
while(fgets(line1, sizeof(line1), file) != NULL) // read one file at a time
{
//printf("%s",line1);
line1[strlen(line1)-1]='\0'; // to remove the last \n
fp = fopen(line1, "r");
if ( fp != NULL)
{
char line[128];
while(fgets(line, sizeof(line), fp) != NULL) // read contents of .phseg linyByLine
{
char *words[10];
int nwords;
nwords = getwords(line, words, 20);
char str[3];
strcpy(str,line2);
if(strcmp(words[3],str)==0)
{
int a1 = atoi(words[0]); // convert string to integer
int a2 = atoi(words[1]);
int a3 = a2 - a1; // get thr number of frames
//printf("%d\t%d\t%d\t",a1,a2,a3);
phone[a3][count]++;
}
}
fclose(fp);
}
else
{
printf("file not found");
}
}
}
count++;
}
}
int j;
for(i=0;i<30;i++)
{
for(j=0;j<noOfLines;j++)
printf("%d\t",phone[i][j]);
printf("\n");
}
}
Please help me out. I am stuck at this point.
Thanx..!!