Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I would like to show data by searching method.
It is working for only first record which meets the entered criteria.
For the next records it's not displaying.

Please help me out.

Thanks in advance

my code
C++
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>

void addrecord();
void viewrecord();
void editrecord();
void deleterecord();
void search();

struct record
{
    char time[6];
    char stud_nm[30];
    char addrs[25];
    char cnt_no;
    char enroll_no[20];
    char sem;
    char dob;
    char year[10];
};

void main()
{

    int ch;

   clrscr();

    printf("\n\n\t***********************************\n");

    printf("\t*SCHOOL MANAGMENT SYSTEM*\n");

    printf("\t***********************************");

   while(1)

	{

		printf("\n\n\t\tMAIN MENU:");

		printf("\n\n\tADD RECORD\t[1]");

		printf("\n\tVIEW RECORD\t[2]");

		printf("\n\tEDIT RECORD\t[3]");

		printf("\n\tSEARCH\t\t[4]");

		printf("\n\tEXIT\t\t[5]");

		printf("\n\n\tENTER YOUR CHOICE:");

		scanf("%d",&ch);

	    switch(ch)

		    {

			    case 1:

			    addrecord();

			    break;

			    case 2:

			    viewrecord();

			    break;

			    case 3:

			      editrecord();

			    break;


			    case 4:
				   search();
				   break;


			    case 5:
			    printf("press any key to go back ");
			    getch();

			    exit(0);

			    default:

			    printf("\nYOU ENTERED WRONG CHOICE..");

			    printf("\nPRESS ANY KEY TO TRY AGAIN");

			    getch();

			    break;

		    }

		system("cls");

	}



}

void addrecord( )

{               FILE *fp ;

		char another = 'Y' ,time[10];

		struct record e ;

		char filename[15];

		int choice;

		system("cls");

	     // printf("application is underconstruction");


		printf("\n\n\t\t***************************\n");

		printf("\t\t* WELCOME TO THE ADD MENU *");

		printf("\n\t\t***************************\n\n");

	    //	printf("\n\n\tENTER DATE OF YOUR RECORD:[yyyy-mm-dd]:");

		fflush(stdin);

	      //	gets(filename);

		fp = fopen ("s.txt", "ab+" ) ;

		if(fp==NULL)

		{

		    printf("\nSYSTEM ERROR...");

		    printf("\nPRESS ANY KEY TO EXIT");

		    getch();

		    return ;

		}



		while ( another == 'Y'|| another=='y' )

		{

		    choice=0;

		    fflush(stdin);

		printf ( "\n\tENTER TIME:[hh:mm]:");

		scanf("%s",time);

		rewind(fp);

		while(fread(&e,sizeof(e),1,fp)==1)

		{

		    if(strcmp(e.time,time)==0)

		    {

			printf("\n\tTHE RECORD ALREADY EXISTS.\n");

			choice=1;

		    }

		}

		    if(choice==0)

		    {

			strcpy(e.time,time);

			printf("\tENTER NAME:");

			fflush(stdin);

			gets(e.stud_nm);

			fflush(stdin);

			printf("\tENTER ENROLLMNT NO:");

			scanf("%s",& e.enroll_no);

			fflush(stdin);

			printf("\tENTER ADDRESS:");

			gets(e.addrs);

			fflush(stdin);

			printf("\tYEAR:");

			scanf("%s",e.year);


			printf("\tSEM:");

			scanf("%s",e.sem);


			printf("\tDATE OF BIRTH:");

			scanf("%s",e.dob);

			fwrite ( &e, sizeof ( e ), 1, fp ) ;

			printf("\nYOUR RECORD IS ADDED...\n");

		    }

			printf ( "\n\tADD ANOTHER RECORD...(Y/N) " ) ;

			fflush ( stdin ) ;

			another = getchar( ) ;

		}

		fclose ( fp ) ;

		printf("\n\n\tPRESS ANY KEY TO EXIT...");

		getch();

    }



void viewrecord( )

{

	FILE *fpte ;
	struct record stud;

	char time[6],choice,filename[14];

	int ch;
	system("cls");


	printf("\n\n\t\t*******************************\n");

	printf("\t\t* HERE IS THE VIEWING MENU *");

	printf("\n\t\t*******************************\n\n");

	do

	    {

		//    printf("\n\tENTER THE DATE OF RECORD TO BE VIEWED:[yyyy-mm-dd]:");

		    fflush(stdin);

		  //  gets(filename);

		    fpte = fopen ("s.txt", "rb" ) ;

		    if ( fpte == NULL )

			    {

				puts ( "\nTHE RECORD DOES NOT EXIST...\n" ) ;

				printf("PRESS ANY KEY TO EXIT...");

				getch();

				return ;

			    }

			    system("cls");

		printf("\n\tHOW WOULD YOU LIKE TO VIEW:\n");

		printf("\n\t1.WHOLE RECORD OF THE DAY.");

		printf("\n\t2.RECORD OF FIX TIME.");

		printf("\n\t\tENTER YOUR CHOICE:");

		scanf("%d",&ch);

		switch(ch)

		{

		    case 1:

			    printf("\nTHE WHOLE RECORD FOR %s IS:","s.txt");

			 while ( fread ( &stud, sizeof ( stud ), 1, fpte ) == 1 )

			 {

			    printf("\n");

			    printf("\nTIME: %s",stud.time);

			    printf("\nENROLLMENT NO: %d",stud.enroll_no);

			    printf("\nSTUDENT NAME: %s",stud.stud_nm);

			    printf("\nADDRESS: %s",stud.addrs);

			    printf("\nYEAR: %d",stud.year);

			    printf("\nSEM: %d",stud.sem);

			    printf("\nDATE OF BIRTH: %d",stud.dob);

			    printf("\n");

			 }

			 break;

		    case 2:

			    fflush(stdin);

			    printf("\nENTER TIME:[hh:mm]:");

			    gets(time);

			    while ( fread ( &stud, sizeof ( stud ), 1, fpte ) == 1 )

			    {

				if(strcmp(stud.time,time)==0)

				{

				    printf("\nYOUR RECORD IS:");

				    printf("\nENROLLMENT NO: %d",stud.enroll_no);

				    printf("\nTIME: %s",stud.time);

				    printf("\nSTUDENT NAME: %s",stud.stud_nm);

				    printf("\nADDRESS: %s",stud.addrs);

				    printf("\nYEAR: %d",stud.year);

				    printf("\nSEM: %d",stud.sem);

				    printf("\nDATE OF BIRTH: %d",stud.dob);



				}

			    }

			    break;

		    default: printf("\nYOU TYPED SOMETHING ELSE...\n");

			     break;

		}

	       printf("\n\nWOULD YOU LIKE TO CONTINUE VIEWING...(Y/N):");

	       fflush(stdin);

	    scanf("%c",&choice);

	}while(choice=='Y'||choice=='y');

	   fclose ( fpte ) ;
	    return ;

		printf("\n\n\tPRESS ANY KEY TO EXIT...");

		getch();

}

void editrecord()

{
	FILE *fpte ;

	struct record r;

	char time[6],choice,filename[14];

	int num,count=0;


	system("cls");

	printf("\n\n\t\t*******************************\n");

	printf("\t\t* WELCOME TO THE EDITING MENU *");

	printf("\n\t\t*******************************\n\n");



	do

	    {

		   // printf("\n\tENTER THE DATE OF RECORD TO BE EDITED:[yyyy-mm-dd]:");

		    fflush(stdin);

		   // gets(filename);

		    printf("\n\tENTER TIME:[hh:mm]:");

		    gets(time);

		    fpte = fopen ("s.txt", "rb+" ) ;

		    if ( fpte == NULL )

			    {

				printf( "\nRECORD DOES NOT EXISTS:" ) ;

				printf("\nPRESS ANY KEY TO GO BACK");

				getch();

				return;

			    }

		    while ( fread ( &r, sizeof ( r ), 1, fpte ) == 1 )

		    {

			if(strcmp(r.time,time)==0)

			{

			    printf("\nYOUR OLD RECORD WAS AS:");


			    printf("\nTIME: %s",r.time);

			    printf("\nENROLLMENT NO: %d",r.enroll_no);

			    printf("\nSTUDENT NAME: %s",r.stud_nm);

			    printf("\nADDRESS: %s",r.addrs);

			    printf("\nYEAR: %d",r.year);

			    printf("\nSEM: %d",r.sem);

			    printf("\nDATE OF BIRTH: %d",r.dob);

		     printf("\n\n\t\tWHAT WOULD YOU LIKE TO EDIT..");

		      printf("\n1.TIME.");

			printf("\n2.STUDENT NAME.");

			printf("\n3.ADDRESS");

			printf("\n4.DATE OF BIRTH.");

			printf("\n5.YEAR.");

			printf("\n6.WHOLE RECORD.");

			printf("\n7.GO BACK TO MAIN MENU.");

				do

				{

				   printf("\n\tENTER YOUR CHOICE:");

				   fflush(stdin);

				   scanf("%d",&num);

				   fflush(stdin);

				   switch(num)

				   {

				      case 1: printf("\nENTER THE NEW DATA:");

					    printf("\nNEW TIME:[hh:mm]:");

					    gets(r.time);

					    break;

				    case 2: printf("\nENTER THE NEW DATA:");

					    printf("\n NEW STUDENT NAME:");

					    gets(r.stud_nm);

					    break;

				    case 3: printf("\nENTER THE NEW DATA:");

					    printf("\nNEW ADDRESS:");

					    gets(r.addrs);

					    break;

				    case 4: printf("\nENTER THE NEW DATA:");

					    printf("\nDATE OF BIRTH:");

					    scanf("%d",&r.dob);

					    break;

				    case 5: printf("ENTER THE NEW DATA:");

					    printf("\nYEAR:");

					    scanf("%d",&r.year);

					    break;

				    case 6:  printf("\nENTER THE NEW DATA:");

					     strcpy(r.time,time);

						printf("\tENTER NAME:");

						fflush(stdin);

						gets(r.stud_nm);

						fflush(stdin);

						printf("\tENTER ENROLLMNT NO:");

						scanf("%d",& r.enroll_no);

						fflush(stdin);

						printf("\tENTER ADDRESS:");

						gets(r.addrs);

						fflush(stdin);

						printf("\tYEAR:");

						scanf("%d",r.year);


						printf("\tSEM:");

						scanf("%d",r.sem);


						printf("\tDATE OF BIRTH:");

						scanf("%d",r.dob);
						 break;

				    case 7: printf("\nPRESS ANY KEY TO GO BACK...\n");

					    getch();

					    return ;

				    default: printf("\nYOU TYPED SOMETHING ELSE...TRY AGAIN\n");

					     break;

					}

				}while(num<1||num>8);

			    fseek(fpte,-sizeof(r),SEEK_CUR);

			    fwrite(&r,sizeof(r),1,fpte);

			    fseek(fpte,-sizeof(r),SEEK_CUR);

			    fread(&r,sizeof(r),1,fpte);

			    choice=5;

			    break;

			}

		    }

		    if(choice==5)

		    {

		    system("cls");

		    printf("\n\t\tEDITING COMPLETED...\n");

		    printf("--------------------\n");

		    printf("THE NEW RECORD IS:\n");

		    printf("--------------------\n");


			    printf("\nTIME: %s",r.time);

			    printf("\nENROLLMENT NO: %d",r.enroll_no);

			    printf("\nSTUDENT NAME: %s",r.stud_nm);

			    printf("\nADDRESS: %s",r.addrs);

			    printf("\nYEAR: %d",r.year);

			    printf("\nSEM: %d",r.sem);

			    printf("\nDATE OF BIRTH: %d",r.dob);

			      fclose(fpte);

		    printf("\n\n\tWOULD YOU LIKE TO EDIT ANOTHER RECORD.(Y/N)");

		    scanf("%c",&choice);

		    count++;

		    }

		    else

		    {

			printf("\nTHE RECORD DOES NOT EXIST::\n");

			printf("\nWOULD YOU LIKE TO TRY AGAIN...(Y/N)");

			scanf("%c",&choice);

		    }

	    }while(choice=='Y'||choice=='y');

	    fclose ( fpte ) ;

	    if(count==1)

	    printf("\n%d FILE IS EDITED...\n",count);

	    else if(count>1)

	    printf("\n%d FILES ARE EDITED..\n",count);

	    else

	    printf("\nNO FILES EDITED...\n");

	    printf("\tPRESS ENTER TO EXIT EDITING MENU.");

	    getch();

}

void search()
{

	FILE *fp;
	struct record sch;

	char nm[100];
	char enroll[20];
	char yr[20];
	int ch,i;


	system("cls");
	printf("\n\tSEARCH STUDENT'S RECORD\n\t");
	printf("\n\t [1].SEARCH BY NAME\n\t");
	printf("\n\t [2].SEARCH BY ENROLLMENT NO\n\t");
	printf("\n\t [3].SEARCH BY YEAR\n\t");

	printf("\nEnter your choice :");
	scanf("%d",&ch);

	switch(ch)
	{
		case 1:

			printf("\nEnter name : ");
			scanf("%s",nm);
		       printf("%s",nm);
			fp=fopen("s.txt","rb");

		      //	while(!feof(fp))
		      while(fread(&sch,sizeof(sch),1,fp)==1)
			{

			  fread(&sch,sizeof(sch),1,fp);

				if(strcmp(nm,sch.stud_nm)==0)
				 {

				    printf("\nYOUR RECORD IS:");

				    printf("\nENROLLMENT NO: %s",sch.enroll_no);

				    printf("\nSTUDENT NAME: %s",sch.stud_nm);

				    printf("\nADDRESS: %s",sch.addrs);

				    printf("\nYEAR: %s",sch.year);

				    printf("\nDATE OF BIRTH: %s",sch.dob);



					 getch();

				}



			}
			  fclose(fp);
			    break;

		case 2:
		      printf("\nEnter Enrollment no :");
		      scanf("%s",enroll);

			fp=fopen("s.txt","rb");

			while(!feof(fp))
			{

			  fread(&sch,sizeof(sch),1,fp);

				if(strcmp(enroll,sch.enroll_no)==0)
				 {

				    printf("\nYOUR RECORD IS:");

				    printf("\nENROLLMENT NO: %s",sch.enroll_no);

				    printf("\nSTUDENT NAME: %s",sch.stud_nm);

				    printf("\nADDRESS: %s",sch.addrs);

				    printf("\nYEAR: %s",sch.year);

				    printf("\nDATE OF BIRTH: %s",sch.dob);

					 getch();

				}

			}

			break;
		 case 3:
			printf("Enter year:");
			scanf("%s",yr);

			fp=fopen("s.txt","rb");

			while(!feof(fp))
			{

			  fread(&sch,sizeof(sch),2,fp);

				if(strcmp(yr,sch.year)==0)
				 {

				    printf("\nYOUR RECORD IS:");

				    printf("\nENROLLMENT NO: %s",sch.enroll_no);

				    printf("\nSTUDENT NAME: %s",sch.stud_nm);

				    printf("\nADDRESS: %s",sch.addrs);

				    printf("\nYEAR: %s",sch.year);

				    printf("\nDATE OF BIRTH: %s",sch.dob);

					 getch();

				}

			}

			break;
	     default:
		      printf("Wrong choice");
		      exit(0);
		      break;
	   }
}
Posted
Updated 2-Jan-14 7:05am
v8
Comments
Richard MacCutchan 27-Dec-13 11:25am    
You are breaking out of the while loop as soon as you find a record containing the name, so it will only print the one entry. You need to continue until the end of the file, and also close and reopen the file with every new search.
ZurdoDev 27-Dec-13 11:27am    
Looks a lot like a solution to me. :)
Richard MacCutchan 27-Dec-13 11:29am    
Well, hardly. There is still quite a lot to be done looking at the code above.
Member 10011989 28-Dec-13 4:08am    
I've close the file BT still the same problems
Richard MacCutchan 28-Dec-13 4:31am    
Since nearly every statement is mis-spelled in the above code I am not sure how you get it to compile. However, you still have a break statement in your while loop, so it will terminate after the first record match.

FP=fread ( & sch,sizeof ( sch),1,FP)

fread() returns a size_t (the size of the fetched data), you're overwriting your file pointer (although, I can't even see in your code where you've even opened the file).
http://www.tutorialspoint.com/c_standard_library/c_function_fread.htm[^]
 
Share this answer
 
Comments
Member 10011989 2-Jan-14 11:16am    
yes albert i've take ur advice in consideration, bt it's not working,, y its not taking value of second record as stud_nm its is annoying me most..
therefore if condition dosent satisfied and it return back to the menu .

can u plz little bit elaborate it so i can solve this as soon as posible
Albert Holguin 2-Jan-14 12:01pm    
Elaborate what? ...it's rather straight forward...
In addition to what Albert said in solution 2, your while condition is wrong. You cannot compare the file pointer directly to EOF. The correct way of doing this is
C++
while (!feof (FP))
{
   ...

Besides, why did you capitalize the beginning of each line? This code won't compile on any C compiler. If you want us to look at your code you should paste it exactly like it is. Otherwise, subtle errors might go undetected.
 
Share this answer
 
v2
Comments
Member 10011989 28-Dec-13 4:40am    
After changing while condition it return back to the main menu bt record is not displaying
nv3 28-Dec-13 5:43am    
of course it should read "while (!feof (FP))".
Member 10011989 2-Jan-14 5:55am    
its not working still for my code.. may be i've made any silly mistake which i cant find..
nv3 2-Jan-14 9:31am    
You did not follow the advice in solution 2, which indicated that the statement
fp=fread(&sch,sizeof(sch),1,fp);
is destroying your file pointer. fread returns the number of elements read and that number should not be assigned to fp.
Member 10011989 2-Jan-14 11:19am    
first of all thanks for notifying my mistake.
ans i've check the solution 2 also..

bt its not working at all
You should start from scratch, check this out:
C Tutorials[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900