1.11M Members

segmentation fault ASAP.

 
0
 

anyone can help me. all of this overcome the segmentation fault.

#include<stdio.h>

 int bubble(int*,int);
 void filewrite();
 void avgmarks();
 void fileprint();
 void filesort();
 void rollin();

int main(void)
{
  int marks[100],rollno[100],x[100],n,i,j,roll,c,mark,roll1;
  char name,nam;

  while(c!=6)
   {  

     printf("GIVE CHOICE--\n");
     printf("   1 TO ENTER STUDENT INFO.\n");
     printf("   2 TO SEE STUDENT.TXT FILE\n");
     printf("   3 TO SORT FILE ON BASIS OF MARKS\n");
     printf("   4 TO PRINT STUDENT INFO. USING ROLL NO\n");
     printf("   5 TO FIND AVERAGE OF MARKS\n");
     printf("   6 TO EXIT\n\n--");
     scanf("%d",&c);

     switch(c)
     {
     case 1:
          filewrite();
          break;
     case 2:
          fileprint();
         break;
     case 3:
         filesort();
         break;
     case 4:  rollin();
          break;
     case 5:  avgmarks();
          break;
     case 6:
          break;
     default:
          break;
     }
    }

  }

int bubble(int x[],int n)
{
 int hold,j,pass,i,switched = 1;
 for(pass = 0; pass < n-1 && switched == 1;pass++)
 {
  switched=0;
  for (j=0;j<n-pass-1;j++)
   if (x[j]>x[j+1])
   {
    switched=1;
    hold = x[j];
    x[j] = x[j+1];
    x[j+1]=hold;
    }
   }
return(0);
}

void filewrite()
{
  int roll,mark,ch;
  char * name;
   FILE *fp;

  fp = fopen("student.txt","a");
   printf("ENTER ROLL NUMBER, NAME , MARKS \n");
  ch = 1;
while (ch)
  {
  scanf("%d%s%d",&roll,name,&mark);
  fprintf(fp,"%d %s %d\n",roll,name,mark);
printf("\n\n press 1 to continue,0 to stop");
   scanf("%d",&ch);

  }

   fclose(fp) ;

}

void fileprint()
{
  int marks[100],rollno[100],x[100],i;
  char *name;
  FILE *fp;


  fp = fopen("student.txt","r");
   i=0;
   printf("ROLLNO       NAME        MARK\n");
   while(!feof(fp))
  {
     fscanf(fp,"%d %s %d\n",&rollno[i],name,&marks[i]);
     printf("%d\t\t%s\t\t%d\n",rollno[i],name,marks[i]);
     i=i+1;
   }
   fclose(fp);
   printf("\n\n\nPRESS ANY KEY");

  }

void filesort()
  { int marks[100],rollno[100],x[100],n,i,j;
    char name;
    FILE *fp,*fm;

    fp = fopen("student.txt","r");
    fm = fopen("marks.txt","w");
    i=0;
   while(! feof(fp))
    {

     fscanf(fp,"%d %s %d\n",&rollno[i],&name,&marks[i]);
     x[i]= marks[i];
     i=i+1;
      }

       n=i;

       bubble(x,n);

    for(i=0;i<n;i++)
    {
    printf(" %d\t",x[i]);
    }

 for(i=0;i<n;i++)
 {
   for (j=0;j<n;j++)
   {
   if(x[i]==marks[j])
   {
      fprintf(fm,"%d %d %d\n",rollno[j],name,marks[j]);
     }
   }
 }
  fclose(fm);
  fclose(fp);
  printf("\n\n\nPRESS ANY KEY");


}

void rollin()
{   int i,roll,ch,mark,roll1;
    char name;
    FILE *fm;

    ch=1;
  while(ch)
  { 
    fm = fopen("marks.txt","r");
    printf(" \n ENTER ROLL NUMBER - ");
    scanf("%d",&roll1);
      i=0;
   while(! feof(fm))
    {
     fscanf(fm,"%d %c %d\n",&roll,&name,&mark);
     if(roll1==roll)
    {printf("\nROLLNO.     NAME        MARKS\n ");
     printf(" %d%c%d\n",roll,name,mark);
     break;
     }
     else
     i=i+1;
      }
  printf("\n\npress 1 to see student info, 0 to return to main menu\n");
  scanf("%d",&ch);
  fclose(fm);
  }
 }

void avgmarks()
 {
    int marks[100],rollno[100],n,i;
    float avg,x;
    char name;
    FILE *fm;
    fm = fopen("marks.txt","r");
    i=0;
   while(! feof(fm))
    {

     fscanf(fm,"%d%s%d\n",&rollno[i],&name,&marks[i]);
     x = x + marks[i];
     i=i+1;
      }
     n = i;
   avg = x/n;
  printf("AVERAGE MARKS OF %d STUDENTS ARE -  %f ",n,avg);
  fclose(fm);
  printf("\n\n\nPRESS ANY KEY");


}
 
0
 

Lines 72, 94, 114, 156 and 187 all need more memory. Try

char name[128];  // Or larger if you need it

Now when you facanf or scanf into name you will have memory. See if that fixes it.

 
0
 

Oh yeah, forgot. Should then look like

fscanf(fm,"%s",name);  // <---- No &name, just name
// or
scanf("%s",name);      // <---- No &name, just name
You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article