I have these structs. and I have two files one is program.dat and other is course.dat and I am supposed to load the values from those .dat files to the struct.
The separation between attributes in program.dat and course.dat is '|'
for example programID|programTitle|ProgramCode........
and I have used string tokenizer and they work perfectly but it doesn't load in link list. The course should add to those program when course's program ID will match with program ID. In other words there is an program Id attribute for each course .But My question is they dont load when i use while loop to print the value I GET SEGMENTATION FAULT ERROR HELP PLEASE

typedef struct course
{
   char courseID[COURSE_ID_LEN + 1];  /* Unique ID for a course */
   char progID[PROG_ID_LEN + 1];
   char courseTitle[MAX_TITLE_LEN + 1];
   char courseCode[COURSE_CODE_LEN + 1];
   unsigned creditPoints;
   TeachPeriodType teachPeriods[NUM_TEACHPERIODS];
   char courseDescription[MAX_DESC_LEN];
   CourseTypePtr nextCourse;
} CourseType;

typedef struct program
{
   char progID[PROG_ID_LEN + 1];   /* Unique ID for a program */
   char progTitle[MAX_TITLE_LEN + 1];
   char progMajor[MAX_TITLE_LEN + 1];
   char progCode[PROG_CODE_LEN + 1];
   float progDuration;
   char progType[PROG_TYPE_LEN];      /* Undergrade(UG) or Postgrad (PG) */
   char progMode[MODE_LEN];  /* Full-time (FT) or Part-time (PT) */
   char progDescription[MAX_DESC_LEN];
   char majorDescription[MAX_DESC_LEN];
   ProgramTypePtr nextProgram;
   CourseTypePtr headCourse;
   unsigned numCourses;
} ProgramType;



typedef struct pms
{
   ProgramTypePtr headProgram;
   unsigned numPrograms;
} PMSType;

This is my implemented code to LoadData

int loadData(PMSType* pms, char* programFile, char* courseFile)
{
	
	
	FILE *fp;
	FILE *fp2;
	char *programLine;
	char *courseLine;
	
    if(!(fp=fopen(programFile, "r"))){
         printf("Error occurered while opening the program.dat file");
         return 0;                             
    }                             
    if(!(fp2=fopen(courseFile,"r"))){
         printf("Error occurered while opening the course.dat file");     
         return 0;                             
     }
      
	char *progId;
	char *progType;
	char *progTitle;
	char *progMajor;
	char *progCode;
	char *progDuration;
	char *progMode;
	char *progDescription;
	char *majorDescription;
	
   char *courseId;
   char *cProgId;
   char *courseTitle;
   char *courseCode;
   char *creditPts;
   char *tPeriod;
   char courseDesc;
   
   ProgramType *prevProgram, *newProgram, *currProgram;
   CourseType *newCourse, *prevCourse, *currCourse;
   
   	int i, j;
	char arra[500][500];
	char line[500]; /* or other suitable maximum line size */ 


	for(i=0; i<500; i++)
	for(j=0; j<500; j++) 
	arra[i][j] = '\0';

	for(i=0; i<500; i++)
	line[i] = '\0';

	if ( fp != NULL )
	{ 

	i=0; 

	while ( fgets ( line, sizeof line, fp ) != NULL ) /* read a line */
	{

	strcpy(arra[i], line);
	printf("array ----> %s ", &arra[i]);
	i++;
	

 	prevProgram=NULL;
	currProgram=pms->headProgram;
        progId = strtok(arra, "|");
	if(progId == NULL)
	{
		printf("Program Id\n");
		break;
	}
	progType = strtok(NULL, "|");
        if(progType==NULL)
	{
		printf("Program Type\n");
		break;
	}
	progTitle=strtok(NULL,"|");
	if(progTitle==NULL)
	{
		printf("Program title\n");
		break;
	}
	progMajor=strtok(NULL,"|");
	if(progMajor==NULL)
	{
	    printf("Major\n");
	    break;
	}	
	progCode=strtok(NULL,"|");
	if(progCode==NULL)
	{
		printf("Program Code\n");
		break;
	}
      
        
	progDuration=strtok(NULL,"|");
	if(progDuration==NULL)
	{
		printf("Duration\n");
		break;
	}
	
	progMode=strtok(NULL,"|");
	if(progMode==0)
	{
		printf("mode\n");
		break;
	}
	
	progDescription=strtok(NULL,"|");
	if(progDescription==NULL)
	{
		printf("program Description\n");
		break;
	}
	majorDescription=strtok(NULL,"|");
	if(majorDescription==NULL)
	{
		printf("major Description\n");
		break;
	}

      /*trying to reach to the last element  of link list*/
     while(currProgram != NULL) 
      {
         if(strcmp(progTitle,currProgram->progTitle) < 0)
         {
            break;
         }
		 
         prevProgram = currProgram;
         currProgram = currProgram->nextProgram;
   
      }  

      newProgram=(ProgramType*)malloc(sizeof(ProgramType));
      newProgram->progCode=progCode;
      newProgram->progID=progId;
      newProgram->progDescription=progDescription;
      newProgram->progDuration=atof(progDuration);
      newProgram->progMajor=progMajor;
      newProgram->progTitle=progTitle;
      newProgram->progType=progType;
      newProgram->majorDescription=majorDescription;
      
      if(prevProgram==0){
         newProgram->nextProgram=pms->headProgram;
         pms->headProgram=newProgram;       
         pms->numPrograms=pms->numPrograms+1;      
        }
      else{
           newProgram->nextProgram=currProgram;
           prevProgram->nextProgram=newProgram;
           pms->numPrograms=pms->numPrograms+1;
           }
      
            

	}
	fclose ( fp );
	}
	else
	{
	perror ( fp ); 
	/* why didn't the file open? */
/*****************************Loading Course*********************/
       	int x, y;
	char array[500][500];
	char lines[500]; /* or other suitable maximum line size */ 


	for(x=0; ix500; x++)
	for(y=0; y<500; y++) 
	array[x][y] = '\0';

	for(x=0; x<500; x++)
	lines[x] = '\0';

	if ( fp != NULL )
	{ 

	x=0; 

	while ( fgets ( line, sizeof line, fp ) != NULL ) /* read a line */
	{ 
                       strcpy(array[x], lines);
                          prevProgram=NULL;
                         currProgram=pms->headProgram;
                         prevCourse=NULL;
                         currCourse=currProgram->headCourse;
                         
                         courseId=strtok(array,"|");
                         cProgId=strtok(NULL,"|");
                         courseTitle=strtok(NULL,"|");
                         courseCode=strtok(NULL,"|");
                         creditPts=atoi(strtok(NULL,"|"));
                         tPeriod=strtok(NULL,"|");
                         courseDesc=strtok(NULL,"|");
                         
    
                      
                         
                         while(currProgram!=NULL)
                         {
                                                 /*stop when program code will be 
                                                 equal to course's program ID
                                                 and course will be added to that program
                                                 */
                                          if(strcmp(currProgram->progIDcProgId)==0)
                                          {
                                                                   break;
                                                                   }
                                                                   
                                          prevProgram=currProgram;
                                          currProgram=currProgram->nextCourse;
                                          }
                                          
                         newCourse = (CourseType*)malloc(sizeof(CourseType));
                         strcpy(newCourse->courseID,courseId);
                         strcpy(newCourse->progID,pCourseId);
                         
                         strcpy(newCourse->courseTitle,courseTitle);
                         strcpy(newCourse->courseCode,courseCode);
                         newCourse->creditPoints=creditPts;
                         strcpy(newCourse->teachPeriods->semester,strok(tPeriod,","));
                         newCourse->teachPeriods->year=strok(NULL,",");
                         
                         strcpy(newCourse->courseDescription,courseDesc);
                         
                                               
                         newCourse->nextCourse=currProgram->headProgram;
                         currProgram->headProgram=newCourse;           
                              
	}
	fclose ( fp );
	}
	else
	{
	perror ( fp ); 
	
	/* why didn't the file open? */

}

}

In the first look there seems to be a problem in the way you create the linked list. According to what I think, you dont have a head pointer. What is the name of the pointer which points to the head of the linked list ?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.