Hi. I'm having problems about my program. My program is something like a directory. It takes in values for different fields and stores it in a linked list in a sorted manner. It also allows the user to save the linked list contents to a text file. Everything runs okay except for the function that checks the file existence and if it exists, it reads the file and stores its contents to a linked list before inserting the new node which contains the recent user-input.
Here is my copyFiletoList and insert function:
void copyFiletoList(nodePtr *start){
nodePtr newPtr, prevPtr, curPtr;
RecordNode temp;
nodePtr fPtr;
char lN[30]="\0";
char fN[30]="\0";
char mN[30]="\0";
char cE[30]="\0";
char sN[30]="\0";
char cN[30]="\0";
fp = fopen("directory.txt", "r");
fPtr = *start;
rewind(fp);
while(!feof(fp)){
fread(&temp, (sizeof(struct recordNode)-1), 1, fp);
fscanf(fp," %[^\n] %[^\n] %[^\n] %[^\n] %[^\n] %[^\n]", temp.Name, temp.fName, temp.mName, temp.course, temp.sNum, temp.cNum);
insert(&fpTr, temp.Name, temp.fName, temp.mName, temp.course, temp.sNum, temp.cNum);
}
fclose(fp);
}
void insert(nodePtr *start, char lName[30], char f[30], char m[30], char c[30], char s[10], char cN[10]){
nodePtr newPtr, prevPtr, curPtr;
newPtr = malloc(sizeof(struct recordNode));
if(newPtr != NULL){
strcpy(newPtr->Name, lName);
strcpy(newPtr->fName, f);
strcpy(newPtr->mName, m);
strcpy(newPtr->course, c);
strcpy(newPtr->sNum, s);
strcpy(newPtr->cNum, cN);
newPtr->nextPtr = NULL;
prevPtr = NULL;
curPtr = *start;
while(curPtr!=NULL && strcmp(lName,curPtr->Name)==1){
prevPtr = curPtr;
curPtr = curPtr->nextPtr;
}
if(prevPtr == NULL){
/*Insert at the beginning*/
newPtr->nextPtr = *start;
*start = newPtr;
}
else{
/*Insert at the middle*/
prevPtr->nextPtr = newPtr;
newPtr->nextPtr = curPtr;
}
}else{
printf("Memory insufficient. Cannot insert.\n");
}
}
The whole program compiles and runs fine without any error, but when I try to display the whole linked list, it only displays the recent data from input even though the file already has contents prior to the run. Please help me. Thank you.