johnwj
johnwj

Reputation: 449

Abort trap:6 for fopen

I'm trying to read in a file called "pg.txt" and print out its content, and I am getting an abort trap:6 error. I don't understand why I am getting it.

Here is my main file:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(){
   char s1[10];
   int d1;
   int n1;
   int n2;
   int n3;
   int n4;

   FILE * fp;

   fp = fopen ("pg.txt", "r");
   int i;
   fscanf(fp, "%d", &d1);
   printf("numTypes |%d|\n", d1 );
   for (i = 0; i < d1; i++){
     fscanf(fp, "%s %d %d %d %d", s1, &n1, &n2, &n3, &n4);
     printf("type1 |%s|\n", s1 );
     printf("Avg CPU |%d|\n", n1 );
     printf("avg burst |%d|\n", n2 );
     printf("avg interarrival |%d|\n", n3 );
     printf("avg io |%d|\n", n4 );
   }

   printf("Before CLOSING\n");
   fclose(fp);

   return(0);
}

and this is my pg.txt file:

2
interactive 20 10 80 5
batch 500 250 1000 10

This is the output:

numTypes |2|
type1 |interactive|
Avg CPU |20|
avg burst |10|
avg interarrival |80|
avg io |5|
type1 |batch|
Avg CPU |500|
avg burst |250|
avg interarrival |1000|
avg io |10|
Before CLOSING
Abort trap: 6

I'm new to C, so any explanation and help will be highly appreciated.

Upvotes: 0

Views: 655

Answers (1)

johnwj
johnwj

Reputation: 449

The problem is s1 didn't have even space allocated. Once s1[10] was changed to s1[12], there was no more abort traps.

Upvotes: 2

Related Questions