I'm writing a program that can let a user input a student record at a time (including name, ID, and department number) until the user wants to stop. In the program I wrote before, I used a linked list to store the information in program, but I want to modify it, so that the information will be stored in an ASCII file. How can I do it? Can anybody help me? Thanks in advance!
Here is the program I wrote:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum
{
Math=1, Engl, Phys, Chem, Hist, Info
} departNum;
typedef struct StudentData
{
char name[51];
unsigned short studentID;
departNum depar;
struct StudentData *next;
} Student;
Student *get_student(void);
void print_StudentData( Student* first);
int main(void)
{
Student *firstLink;
firstLink = NULL;
firstLink = get_student();
print_StudentData( firstLink);
return (0);
}
Student *get_student(void)
{
Student *newLink, *firstLink, *lastLink;
int choose;
newLink = firstLink = lastLink = NULL;
printf("\nPlease enter 1 to add a student data or enter 2 to exit :");
scanf("%d", &choose);
while ( choose == 1)
{
newLink = (Student*) malloc(sizeof(Student));
printf("\nPlease enter a new Student Name : ");
gets(newLink->name);
gets(newLink->name);
printf("Please enter a new Student ID number : ");
scanf("%d", &(newLink->studentID));
printf("Please enter a Department Number\nMath=1, English=2, Physics=3, Chemistry=4, History=5, Infomation=6 : ");
scanf("%d", &(newLink->depar));
newLink->next = NULL;
if (!firstLink)
{
firstLink = lastLink = newLink;
}
else
{
lastLink->next = newLink;
lastLink = newLink;
}
printf("\nPlease enter 1 to add a student data or enter 2 to exit :");
scanf("%d", &choose);
}
return firstLink;
}
void print_StudentData( Student *firstLink)
{
Student *currLink;
char* departmentName[] = {"", "Math", "English", "Physics", "Chemistry", "History", "Information"};
currLink = firstLink;
printf("\n\nName\t\tID\tDe
partment\n
");
printf("------------------
----------
----------
----------
----\n");
while(currLink)
{
printf("%s", currLink->name);
printf("\t\t%d", currLink->studentID);
printf("\t%s\n",department
Name[currL
ink->depar
]);
currLink = currLink->next;
}
}