Code:
/****************************************************************
FILE NAME: lab4.cpp
DESCRIPTION: This program will ask the user if the want to read a file or input
data. Check the file to see if there is data if that is the first
request. IF not data go back to the options. Have user add data.
Send that data to an ordered linked list then save the data to a
file. Also have the options to show data from file or quit. .
DATE: October 22, 2006
DESIGNER: Tabatha Mitchell
ASSIGNMENT: Lab4
FUNCTIONS: PERSON* InsertNode (PERSON *Top, PERSON *Node )
amend_file
show_file
********************************************************************/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#define Nlen 26
typedef struct person_t { //structure that will sort that data for PERSONS
char FName[Nlen];
char LName[Nlen];
char Sex;
char Grade;
struct person_t *Next;
} PERSON;
struct person_t *Top = NULL; //used for the InsertNode set to empty
struct person_t *Node = NULL; //used for the InsertNode set to empty
/****************************************************************
FUNCTION: PERSON* InsertNode
DESCRIPTION: put the sturctured list into the ordered linked list
INPUT:
Parameters: The parameters as going to be the last name in the structured
list it is going to take the last name and sort it into an
ordered linked list
Keyboard: none
File: none
OUTPUT:
Return Value: last name in an ordered linked list
Parameters: last name ordered linked list
Display: none
File: none
CALLS TO: none
***************************************************************/
PERSON* InsertNode (PERSON *Top, PERSON *Node )
{
PERSON *Here, /* Node being compared to new node */
*Prev; /* Node in front of Here node */
if (Top == NULL)/* List is empty - Place new node at top of list */
Top = Node;
else { /* Top != NULL *//* Start at Top of list */
Here = Top;
strcmp (Here->LName , Node->LName);
Node->Next = Top;
Top = Node;
// else {/* Search for place to insert new node */
// Prev = Top;
// Here = Prev->Next;
while ( (Here != NULL) &&(Here->LName < Node->LName) ) {
Prev = Here;
Here = Here->Next;
}/* Insert into list at found spot */
Node->Next = Prev->Next;
Prev->Next = Node;
} /* end of Top != NULL */
return Top;
}
/*************************************************************
end of Function to Ordered linked list
***************************************************************/
void amend_file( char file[],PERSON *STU );//used to define amend_file function
void show_file( char file[],PERSON *STU ); //used to define show_file function
int main (void)
{
PERSON *STU;
int Ans;
char file[] = "data.txt";
int Records [100];
FILE *fin = fopen(file, "a+");
struct person_t *Top = NULL;
struct person_t *Node = NULL;
do
{
printf("\n\n"
"1. Enter new record\n"
"2. Display List\n"
"3. Quit\n\n");
printf("Prompt: ");
fflush(stdout);
if ( scanf("%d", &Ans) == 1 ) /* get answer */
{
STU = malloc (sizeof (person_t)); //allocate memory space for the STU
if (STU == NULL){ //if not enough space tell us out of memory
printf("Out of memory");
exit(0);
}
fgets(STU->FName,sizeof(STU->FName),stdin);
if (STU == NULL)
{
perror("PERSON");
exit(EXIT_FAILURE);
}
switch(Ans)
{
case 1:
amend_file(file, STU); //calls the amend file function
break;
case 2:
show_file(file, STU); //calls the show file function
break;
case 3: default:
Ans = 3;
break;
}
} /* /get answer */
}
while ( Ans != 3 );
fflush(stdout);
free(STU);
getchar ();
return EXIT_SUCCESS;
}
/****************************************************************
FUNCTION: amend_file
DESCRIPTION: this will get the input for the use, send it to the linked list
and then save it to the file.
INPUT:
Parameters: Person structure. and Person Insertnode
Keyboard: all of the following will be saved in the structure PERSON
LName= last name
FName = first name
Sex= what gender the person is
Grade = The persons grade
File: data.txt this is where the data is saved.
OUTPUT:
Return Value: Send The PERSONS STU to the InsertNode put data into list the
save to file
Parameters: InsertNode, LName, FName, Sex, Grade
Display: none
File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
Sex, Grade
CALLS TO: List of all user-generated functions called
***************************************************************/
void amend_file( char file[], PERSON *STU )
{
int i;
assert(STU != NULL);
STU = (struct person_t *) malloc(sizeof(person_t));
FILE *fin = fopen(file, "a+");
if ( fin == NULL )
{
perror(file);
exit(EXIT_FAILURE);
}
printf("Enter new record: ");
printf ("\nEnter student's First name ( up to %d letters):", Nlen);
printf ("\nEnter student's Last name ( up to %d letters):", Nlen);
printf ("\nPlease enter Sex");
printf ("\nPlease enter Grade\n");
scanf("%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade);
InsertNode (Top, Node);
fprintf(fin, "%s %s %c %c\n", STU->LName, STU->FName, STU->Sex, STU->Grade);
fflush(fin);
fclose(fin);
}
/****************************************************************
FUNCTION: show_file
DESCRIPTION: Reads the data from the file and displays data after sorting if
needed.
INPUT:
Parameters: Person structure. and Person Insertnode these are read from the
file and then put into the list then displayed.
Keyboard:
File: data.txt this is where the data is saved.
OUTPUT:
Return Value: Send The PERSONS STU to the InsertNode put data into list the
save to file
Parameters: InsertNode, LName, FName, Sex, Grade
Display: the order list of people that are saved to the file.
File: data.txt. the PERSONS STU in ordered link list format Lname, FName,
Sex, Grade
CALLS TO:
***************************************************************/
void show_file ( char file[], PERSON *STU )
{
assert(STU != NULL);
int b = 1;
FILE *fin = fopen(file, "rb");
if (file == NULL)
{
perror(file);
exit(EXIT_FAILURE);
}
while (fscanf(fin, "%s %s %c %c", STU->FName, STU->LName, &STU->Sex,
&STU->Grade)== 4)
{
InsertNode (Top, Node);
b++;
printf("Record %d: %s %s %c %c\n", b, STU->FName, STU->LName,
STU->Sex, STU->Grade);
}
fclose(fin);
}