Dynamic Linked Lists and File Manipulation/assign5.c
void saveStudentInfo(StudentInfoPtr *head) {
    
    char *fileName = "inputStudents.txt";
    FILE *filePtr = NULL;
    
    if((filePtr = fopen(fileName, "w")) == NULL) {
        puts("ERR IN SAVING TO FILE | FILE COULD NOT BE OPENED");
    }
    else {
        
        StudentInfoPtr currentStudentInfoPtr = *head;
        
        //writes the student infos to a file
        while(currentStudentInfoPtr != NULL) {
            
            fprintf(filePtr, "%s\n%s\n%s\n%d\n", 
                                currentStudentInfoPtr->studentId,
                                currentStudentInfoPtr->firstName,
                                currentStudentInfoPtr->lastName,
                                currentStudentInfoPtr->numOfAttentingCourses);
                                
            for(int i = 0; i < currentStudentInfoPtr->numOfAttentingCourses; i++){
                fprintf(filePtr, "%s %d\n", currentStudentInfoPtr->courseInfoArr[i].courseName, currentStudentInfoPtr->courseInfoArr[i].courseId);
            }            
            currentStudentInfoPtr = currentStudentInfoPtr->next;
        }
        
        fclose(filePtr);
    }
}
/*
    saveStudentInfo: saves the linked list to a file
    input: the beginning of the linked list (head)
*/
saves the linked list to a file
input: the beginning of the linked list (head)
saves the linked list to a file input : the beginning of the linked list ( head )
void saveStudentInfo(StudentInfoPtr *head) {
    char *fileName = "inputStudents.txt";
    FILE *filePtr = NULL;
    if((filePtr = fopen(fileName, "w")) == NULL) {
        puts("ERR IN SAVING TO FILE | FILE COULD NOT BE OPENED");
    }
    else {
        StudentInfoPtr currentStudentInfoPtr = *head;
        while(currentStudentInfoPtr != NULL) {
            fprintf(filePtr, "%s\n%s\n%s\n%d\n", 
                                currentStudentInfoPtr->studentId,
                                currentStudentInfoPtr->firstName,
                                currentStudentInfoPtr->lastName,
                                currentStudentInfoPtr->numOfAttentingCourses);
            for(int i = 0; i < currentStudentInfoPtr->numOfAttentingCourses; i++){
                fprintf(filePtr, "%s %d\n", currentStudentInfoPtr->courseInfoArr[i].courseName, currentStudentInfoPtr->courseInfoArr[i].courseId);
            }            
            currentStudentInfoPtr = currentStudentInfoPtr->next;
        }
        fclose(filePtr);
    }
}
void saveStudentInfo ( StudentInfoPtr * head ) { char * fileName = " " ; FILE * filePtr = NULL ; if ( ( filePtr = fopen ( fileName , " " ) ) == NULL ) { puts ( " " ) ; } else { StudentInfoPtr currentStudentInfoPtr = * head ; while ( currentStudentInfoPtr != NULL ) { fprintf ( filePtr , " \n \n \n \n " , currentStudentInfoPtr -> studentId , currentStudentInfoPtr -> firstName , currentStudentInfoPtr -> lastName , currentStudentInfoPtr -> numOfAttentingCourses ) ; for ( int i = 0 ; i < currentStudentInfoPtr -> numOfAttentingCourses ; i ++ ) { fprintf ( filePtr , " \n " , currentStudentInfoPtr -> courseInfoArr [ i ] . courseName , currentStudentInfoPtr -> courseInfoArr [ i ] . courseId ) ; } currentStudentInfoPtr = currentStudentInfoPtr -> next ; } fclose ( filePtr ) ; } }
saveStudentInfo: saves the linked list to a file
input: the beginning of the linked list (head)
saveStudentInfo : saves the linked list to a file input : the beginning of the linked list ( head )
//writes the student infos to a file