Thread: Help with some error...(linked list)

  1. #1
    Registered User
    Join Date
    Nov 2006

    Help with some error...(linked list)

    I want to read data from a file and parse it into a linked list.But after i had coded the program, it got few erros that i can figure it out. Anyone here can help me for that?

    This are the data from the file:
    0602R00001,Alex Zhu,12.5,20.5,35,68,CR
    0602R12345,Bobby Brain,14.5,24.5,59.5,98.5,HD
    0602R98765,Charles Laz,7,0,0,0,F
    0602R87654,Daniel Dun,8
    0602R22222,Eyvonne Young,10

    Part of my code that reading the file and parse the data
    typedef struct studentNode
       char id[ID_MAX + 1];
       char name[NAME_MAX + 1];
       float mark[MARK_MAX];
       char grade[GRADE_MAX + 1];
       struct studentNode* nextStudent;
    } StudentNode;
    typedef struct studentList
       Components comp[MARK_MAX - 1];
       StudentNode* head;
    } ClassList;
    * Parse each line in student data file into student node 
    int parseText(char * buf, StudentNode * temp){
    	int i;	
    	buf = strtok(buf, ",");
    	for(i=0; buf != NULL; i++)
    		if(i==0)temp->id = buf;
    		if(i==1)temp->name = buf;
    		if(i>1 && i<6)temp->mark[i-2] = buf;
    		if(i==6)temp->grade = buf;
    	temp->nextStudent = NULL;
    	return 0;
    * Function loadstudent() is used to load the student data file, and parse the file to linked list
    void loadStudent(ClassList* student, char* fileName){
    	FILE *fp;
    	StudentNode *head;
    	StudentNode *temp = NULL;
    	StudentNode *next = NULL;
    	char buf[50] = "";
    	int record = 0;
    	fileName = strcat(fileName, ".mrk.dat");
    	if( (fp = fopen(fileName, "r")) != NULL)
    		printf("\nFile Found.");
    		fp = fopen(fileName, "r");
    		head = (StudentNode *) malloc (sizeof (StudentNode));
    		temp = (StudentNode *) malloc (sizeof ( StudentNode));
    		next = (StudentNode *) malloc (sizeof ( StudentNode));
    		if ((fread (buf, 50, 1, fp)) == 0)
    			head = NULL;
    		parseText(buf, head);
    		temp = head;
    		while (fread (buf, 50, 1, fp) != 0)
    			printf("%s", temp->id);
    			parseText(buf, next);
    			temp->nextStudent = next;
    		temp->nextStudent = NULL;
    		if(record == 0)
    			printf("Database Empty");
    			printf("\nContains %d record.", record);	
    		printf("\nFile Not Found.\n");
    After i compile, it occurs 4 erros. (bold line in my code)

    assign2.c:192: incompatible types in assignment
    assign2.c:193: incompatible types in assignment
    assign2.c:194: incompatible types in assignment
    assign2.c:195: incompatible types in assignment

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >if(i==0)temp->id = buf;
    >if(i==1)temp->name = buf;
    >if(i>1 && i<6)temp->mark[i-2] = buf;
    >if(i==6)temp->grade = buf;
    id, name, mark, and grade are all arrays. buf is a pointer. If somebody told you that arrays and pointers are the same, or interchangeable, they were wrong. Try using strcpy instead of direct assignment. Also, before you come running back with an infinite loop, note that you aren't calling strtok again inside the body of your for loop.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    thanks bro@
    after i use strcpy..

    this will occur error with error message "incompatible type for argument 1 of `strcpy' "

    And even i excluded strcpy(temp->mark[i-2],buf); part, my code can be compiled, and when i run it, it stop at "Database Found" only, and will not prints out the info that i need.
    printf("%s", temp->id);
    Any idea why@? i cant figure it out..brain stuck already..

    Also, before you come running back with an infinite loop, note that you aren't calling strtok again inside the body of your for loop.
    It means that??...

  4. #4
    Eager young mind
    Join Date
    Jun 2006
    > float mark[MARK_MAX];

    In your strcpy , both the arguments in that function must be of the type char *..
    which is not the case in your program
    In the middle of difficulty, lies opportunity

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    I've also noticed you don't quite seem to have a clear grasp of what @ is actually for.

    Hope is the first step on the road to disappointment.

  6. #6
    Registered User SKeane's Avatar
    Join Date
    Sep 2006

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. instantiated from here: errors...
    By advocation in forum C++ Programming
    Replies: 5
    Last Post: 03-27-2005, 09:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. List class
    By SilasP in forum C++ Programming
    Replies: 0
    Last Post: 02-10-2002, 05:20 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM