I got my program to work but it seems it cannot:

1.cant read a file if its only one archive (needs two to be able to read)
2.can only read the first line in a FILE
3.delete function deletes more than one than intended.

help are appreciated :)

I have attached the file. thank you

Attachments
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

void mainMenu(void);
void studentMenu(void);
void courseMenu(void);
void resultMenu(void);

void addStudent(void);
void editStudent(void);
void searchStudent(void);
void deleteStudent(void);
void displayStudent(void);

void addSubject(void);
void editSubject(void);
void searchSubject(void);
void deleteSubject(void);
void displaySubject(void);

void addResult(void);
void editResult(void);
void searchResult(void);
void deleteResult(void);
void displayResult(void);
 

int main()
{

	mainMenu();	//function
	system("cls");
	return 0;

}


void mainMenu(void)
{
     
	int choice;
	system("cls");

	printf("\n**********************************************\n");
	printf("\n			STUDENT ADMINISTRATIVE SYSTEM		\n");
	printf("\n**********************************************\n");
	printf("\n	[1]	Student Section						    \n");
	printf("\n	[2]	Student Syllabus Section			    \n");
	printf("\n	[3]	Results Section					\n");
	printf("	[4]	Exit									\n");
	printf("\n\n");
	printf("\nPlease enter your choice :");
	scanf("%d",&choice);
	
	switch(choice)
	{
	case 1:		studentMenu();
				break;
	case 2:		courseMenu();
				break;
    case 3:		resultMenu();
				break;*/
	case 4:		system("cls");
				printf("Exiting Student Administrative System");
                break;
	default:	printf("Invalid selection ");
				break;


	}
}

void studentMenu(void)
{

	int choice;
	system("cls");

	printf("\n**********************************************\n");
	printf("\n			STUDENT MENU						\n");
	printf("\n**********************************************\n");
	printf("\n	[1]	Add New Student							\n");
	printf("	[2]	Edit Existing Student					\n");
	printf("\n	[3]	Display All Student						\n");
	printf("	[4]	Search Student Records					\n");
	printf("\n	[5]	Delete Student Records					\n");
	printf("\n	[6]	Return to Main Menu					    \n");
	printf("\n\n");											
	printf("\nPlease enter your choice :");
	scanf("%d",&choice);

	switch(choice)
	{
	case 1:		addStudent();
				break;
	case 2:		editStudent();
				break;
	case 3:		displayStudent();
				break;
	case 4:		searchStudent();
				break;
	case 5:		deleteStudent();
				break;
	case 6:		mainMenu();
				break;
	default:	printf("Invalid selection ");
				break;
	
	}

}

void addStudent(void)
{
	char studentID[60] = {'\0'};
	char studentName[60] = {'\0'};
	char studentGender[60] = {'\0'};
	char studentDOB[60] = {'\0'};
	char studentAddress[60] = {'\0'};
	char studentPhone[60] = {'\0'};
	char studentGroup[60] = {'\0'};
	char answer;
	int done = 0;
	FILE *bFile;
	bFile = fopen("student.dat","a+");
	system("cls");
	
	


	printf("*************** ADDING NEW STUDENT ******************\n\n");

do
{

	printf("\nAdd Student ID : ");
	fflush(stdin);
	gets(studentID);

	printf("\nEnter Student Name : ");
	fflush(stdin);
	gets(studentName);

	printf("Enter Student Gender : ");
	fflush(stdin);
	gets(studentGender);

	printf("Enter Student Date of Birth : ");
	fflush(stdin);
	gets(studentDOB);

	printf("Enter Student Address : ");
	fflush(stdin);
	gets(studentAddress);
	
	printf("Enter Student Phone Number : ");
	fflush(stdin);
	gets(studentPhone);
	
	printf("Enter Student Group Code : ");
	fflush(stdin);
	gets(studentGroup);
	
	
	fprintf(bFile,"%s,%s,%s,%s,%s,%s,%s,\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);

	
	printf("\n Record Saved. Continue to enter another record? Y = Yes, N = No :");
	fflush(stdin);
	scanf("%c",&answer);

	if(answer == 'Y' || answer == 'y')
				done = 0;
	else
				done = 1;
} while (done != 1);

 
	fclose(bFile);

if (done)
mainMenu();

}
void editStudent()
{
     char studentID[20] = {'\0'}; 
    char studentName[60] = {'\0'};
	char studentGender[10] = {'\0'};
	char studentDOB[30] = {'\0'};
	char studentAddress[60] = {'\0'};
	char studentPhone[60] = {'\0'};
	char studentGroup[60] = {'\0'};
	char target [20] = {'\0'};
	char answer, cont;
 	int found = 0;
 	int choice = 0, loop = 0;
 	
 		FILE *bFile, *temp;

	system("cls");
	printf("\n\n");
	printf("**************************EDIT STUDENT RECORD**************************** ");
	
	temp = fopen("temp.dat","w");

	if((bFile = fopen("student.dat","r")) == NULL)
		printf("File could not be opened ");
	else
	{
         printf("\nEnter ID of the student : ");
		fflush(stdin);
		gets(target);

		while (!feof(bFile))
		{	
            fscanf(bFile,"%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);
			
			if(feof(bFile))
				break;

			if (strcmp(target,studentID) != 0)
				fprintf(temp,"%s,%s,%s,%s,%s,%s,%s,\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);
			else
			{
                found = 1;
                printf("\nStudent ID			: %s",studentID);
                printf("\nStudent Name			: %s",studentName);
                printf("\nStudent Gender			: %s",studentGender);
                printf("\nStudent Date of Birth		: %s",studentDOB);
                printf("\nStudent Address			: %s",studentAddress);
                printf("\nStudent Phone Number		: %s",studentPhone);
                printf("\nStudent Group			: %s",studentGroup);
                printf("Do you want to modify this record ? Y/N : ");
				fflush(stdin);
				scanf("%c",&answer);

				if (toupper(answer) == 'Y')
				{	
					do
					{	
                             printf("\n\n");
                             printf("*************************************\n");
                             printf("Which record do you want to EDIT?\n");
                             printf("*************************************\n");
                             printf("1. Student ID \n");
                             printf("2. Student Name \n");
                             printf("3. Student Gender \n");
                             printf("4. Student Date of Birth \n");
                             printf("5. Student Address \n");
                             printf("6. Student Phone \n");
                             printf("7. Student Group \n");
                             printf("Please Choose [1 to 7]: ");
                             fflush(stdin);
                             scanf("%d",&choice);
						
						do
						{	loop = 0;
                                    switch(choice)
                                {
                                    case 1:
                                           printf("\n\n");
                                           printf("\n\tStudent ID :");
                                           fflush(stdin);
                                           gets(studentID);
                                           break;
                                    case 2:
                                           printf("\n\n");
                                           printf("\n\tStudent Name :");
                                           fflush(stdin);
                                           gets(studentName);
                                           break;
                                    case 3:
                                           printf("\n\n");
                                           printf("\n\tStudent Gender :");
                                           fflush(stdin);
                                           gets(studentGender);
                                           break;
                                    case 4:
                                           printf("\n\n");
                                           printf("\n\tStudent Date of Birth :");
                                           fflush(stdin);
                                           gets(studentDOB);
                                           break;
                                    case 5:
                                           printf("\n\n");
                                           printf("\n\tStudent Address :");
                                           fflush(stdin);
                                           gets(studentAddress);
                                           break;
                                    case 6:
                                           printf("\n\n");
                                           printf("\n\tStudent Phone :");
                                           fflush(stdin);
                                           gets(studentPhone);
                                           break;
                                    case 7:
                                           printf("\n\n");
                                           printf("\n\tStudent Group :");
                                           fflush(stdin);
                                           gets(studentGroup);
                                           break;
                                    default:
                                            printf("\n\nInvalid Choice entered!\n\n");
                                            printf("Please choose [1 to 7] Again: ");
                                            fflush(stdin);
                                            scanf("%d",&choice);
                                            loop = 1;
                                            break;
							}
						}while(loop == 1);
						printf("\n\n");
						printf("Do you still want to continue editing? \n['N' to stop editing, anything to continue] : ");
						fflush(stdin);
						scanf("%c", &cont);

					}while( toupper(cont) != 'N');
					printf("\nRecord Saved\n\n");
				}
				fprintf(temp,"%s,%s,%s,%s,%s,%s,%s,\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);
			}
		}
	}
	fclose(bFile);
	fclose(temp);
	if(found)
	{
		remove("student.dat");
		rename("temp.dat","student.dat");
	}
	else
	{	printf("******************************************************\n");
		pri

It would be helpful to tell us the functions that are giving you problems. I'm not going to read all that code to try and guess what you want.

Thank you for replying.

There are 5 main functions;

1.add
2.edit
3.display
4.search
5.delete

add function & display function works but when it comes to other functions for e.g search function the program only reads the first file in the file and it gives error if there are no more than one item in the file..

the delete funtion works a bit but somehow it deleted 2 files at one time.. don't know whats wrong with my code

I opened it and.... well - 38KB of code! You need to isolate your problem, and not just give a whole project to someone else, to track.
This is also the C++ forum.

Are you missing something here(the brackets)?:

if(answer =='Y' || answer =='y')
				{
				remove("subject.dat");
				rename("temp.dat","subject.dat");
				printf("\nThe Record is deleted\n\n");
                }
                
           {	
			remove("temp.dat");
			printf("\nThe Record is NOT deleted\n\n");
		}

Sry i am new here or to any programming forums.

i dont see any problem with the codes you highlighted.

void searchStudent(void)
{

	char studentID[60] = {'\0'}; 
    char studentName[60] = {'\0'};
	char studentGender[60] = {'\0'};
	char studentDOB[60] = {'\0'};
	char studentAddress[60] = {'\0'};
	char studentPhone[60] = {'\0'};
	char studentGroup[60] = {'\0'};
	char target [20] = {'\0'};
	char answer;
 	int found = 0;
 	
	FILE *bFile;
    bFile = fopen("student.dat","r");
	system("cls");
	printf("\n");
	

printf("**************************SEARCH STUDENT RECORD**************************** ");

	   if (bFile == NULL)
                 printf("File could not be opened ");

else
    printf("\n Enter the ID of the student you want to search :");
	fflush(stdin);
	gets(target);

	while (!feof(bFile) && found == 0)
	{
						
		fscanf(bFile,"%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);

		if (strcmp(target,studentID) == 0)
		{
					found = 1;

					fprintf(bFile,"%s,%s,%s,%s,%s,%s,%s,\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);
		}

	}

		if (found)
		{
			printf("\nStudent ID			: %s",studentID);
			printf("\nStudent Name			: %s",studentName);
			printf("\nStudent Gender			: %s",studentGender);
			printf("\nStudent Date of Birth		: %s",studentDOB);
			printf("\nStudent Address			: %s",studentAddress);
			printf("\nStudent Phone Number		: %s",studentPhone);
			printf("\nStudent Group			: %s\n",studentGroup);
      }
      else
      printf("Record does not exist!\n\n");
      fclose(bFile);
      system("pause");
      mainMenu();
      
}

It's just if() {} {} , shouldn't there be an 'else' between it?

Look again :)

:D lol, I think the doctor lied about these glasses.

ah that one, I've already corrected it. :)

the problem is my search function doesnt detect the second item in my FILE.

{

	char studentID[60] = {'\0'}; 
    char studentName[60] = {'\0'};
	char studentGender[60] = {'\0'};
	char studentDOB[60] = {'\0'};
	char studentAddress[60] = {'\0'};
	char studentPhone[60] = {'\0'};
	char studentGroup[60] = {'\0'};
	char target [20] = {'\0'};
	char answer;
 	int found = 0;
 	
	FILE *bFile;
    bFile = fopen("student.dat","r");
	system("cls");
	printf("\n");
	

printf("**************************SEARCH STUDENT RECORD**************************** ");

	   if (bFile == NULL)
                 printf("File could not be opened ");

else
    printf("\n Enter the ID of the student you want to search :");
	fflush(stdin);
	gets(target);

	while (!feof(bFile) && found == 0)
	{
						
		fscanf(bFile,"%[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c %[^,]%*c",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);

		if (strcmp(target,studentID) == 0)
		{
					found = 1;

					fprintf(bFile,"%s,%s,%s,%s,%s,%s,%s,\n",studentID,studentName,studentGender,studentDOB,studentAddress,studentPhone,studentGroup);
		}

	}

		if (found)
		{
			printf("\nStudent ID			: %s",studentID);
			printf("\nStudent Name			: %s",studentName);
			printf("\nStudent Gender			: %s",studentGender);
			printf("\nStudent Date of Birth		: %s",studentDOB);
			printf("\nStudent Address			: %s",studentAddress);
			printf("\nStudent Phone Number		: %s",studentPhone);
			printf("\nStudent Group			: %s\n",studentGroup);
      }
      else
      printf("Record does not exist!\n\n");
      fclose(bFile);
      system("pause");
      mainMenu();
      
}

anything wrong here?

I think you are missing { and } in the code beginning on line 26

Also, you use fflush(stdin) a lot -- not a good thing to do. See this thread. You might also want to follow the other links shown at the bottom of that page.

It still doesn't work, the search and delete functions uses search function to find through ID, it cant seem to detect 2nd or 3rd item in file

anything wrong here?

Yes. In addition to what the Dragon mentioned,
1) you need to fix your formatting so we can follow your code;
2) you are using feof() incorrectly. This will cause you problems when the function seems to be working;
3) never, ever use gets(). Gives you a major problem in the wrong hands;
Read the links...

Are you sure you are reading the values correctly? And why are you calling mainMenu() at the end of the function?

In order to go back to the main menu after the function does it work. the search works now.

The edit and delete now give problems. The delete function deletes more than one intended. I can't use advanced syntax as because my level of C is basic only, I'm following the notes given by the lecturer.

This article has been dead for over six months. Start a new discussion instead.