Example #1
0
void main()
{
	int leave = 0;
	struct student stu;
	stu.matrix = 0;
	while(!leave)
	{
		char op[10];
		enum CHOICE choice;
		printf("\nWelcome to this incredibly useful tool: \n");
		printf("In order to enter a student detail, choose 0\n");
		printf("I norder to print the details, choose 1\n");	
		printf("If instead you wish to exit, please key in 2:   ");
		scanf("%s", &op);
		choice = atoi(op);
		
		switch(choice)
		{
			case ENTER:
				stu = enterDetails(stu);
				break;
			case PRINT:
				printDetails(stu);
				break;
			case EXIT:
				leave = 1;
				break;
			default:
				printf("Invalid input, try again.\n");
		}
	}
}
int main(void)
{
	FILE *studentFile;
	char option;
	struct student_details *studentPointer;
	studentPointer = (struct student_details*) malloc(sizeof(struct student_details));
	
	if (studentPointer == NULL)
	{
		printf("ERROR: Not enough memory to run this program\n");
		exit(2);
	}
	
	printf("\nWould you like to view Student Details 'v' or overwrite them 'o'?\n");
	scanf("%1c", &option);
	
	/****************** SECTION TO READ FROM FILE ***************************/
	if (option == 'v')
	{
		printf("\nFile contents are:\n\n");
		readFile(studentFile);	//CALL FILE READER FUNCTION
		printf("\n\n");
	}
	
	/****************** SECTION TO WRITE TO STRUCT AND THEN TO FILE *********/
	else if (option == 'o')
	{
		getchar();
		printf("\nFile will be changed from:\n");
		readFile(studentFile);	//CALL FILE READER FUNCTION
		printf("\n\n");
		enterDetails(studentPointer);	//CALL FUNCTION TO WRITE TO STRUCT
		writeFile(studentFile, studentPointer);	//CALL FUNCTION TO WRITE TO FILE
		
		printf("\nFile contents have been changed to:\n");
		readFile(studentFile);	//CALL FUNCTION TO READ FILE AFTER CHANGES HAVE BEEN MADE
		printf("\n\n");
	}
	
	else printf("Invalid operator selected, Please try again.\n");

	free(studentPointer);
	
	return 0;
}