Thread: Problem with sorting

  1. #31
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by preeengles View Post
    I think I'll just leave it at that. I'm more concerned with the bug in my code. There's something wrong with it. You may have noticed that when you first run my program (if you have run it), it has a wrong output. You try deleting all records and try displaying it again and you will see what's wrong with this sorter. But I don't know which part to change.
    This is kind of cherry picking since I haven't been following the discussion, but you are at cross purposes:
    • you want to leave stuff the way you finished it even though it is poorly organized, because that will be easier
    • you want to track down a bug, which is much easier if your code is coherently organized into coherent parts, as opposed to a patchwork of things that happened as you thought of doing them

    This problem is only going to be compounded if you are looking for help from others. It's not just a "programming forum" issue either; if you ever hope to work as part of a team on something, how do you think your peers will feel about having to waste their time compensating for your idiosyncracies?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #32
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your sorter is stuffed, because it isn't really a sorter. It's a piano, as near as I can figure.

    You don't just say "Today, I'll code up a new sorter", and it will be ok.

    Even the pro's can goof up with that idea.

    This is my adaption of your program, with an added sort function, using an array.

    Two functions are new: sorter() and getSize().

    As MK noted, you can do this different ways, but you shouldn't be doing it every way, at once.

    Pick the way you want to do it, and stick with it. If it can't do what you want, you will have learned a lot, just by trying it out. It's not a huge investment of time, unless you want to make "custom" sorters and such.

    Use standard sorters, instead. Sorting programs are fragile, and it's easy to muck them up, beyond comprehension.

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    /****************************************************************************/
    
    typedef struct student
      {
      long long int snum;
      char password[50];
      char lname[31];
      char fname[31];
      char mi[3];
      char bday[7];
      char course[6];
      }record;
    
    /****************************************************************************/
    
    //DECLARATION OF MAJOR FUNCTIONS
    
    void add_record(void);
    void compress(void);
    void disp_all(void);
    void modifySR(void);
    void delSR(void);
    void delAll(void);
    void sorter(void);
    FILE* fp;
    
    /***************************************************************************/
    
    //MAIN
    
    int main()
    {
       int menu_choice;
       record stud;
       if((fp=fopen("std.dat","rb+")) == NULL) {
          if((fp = fopen("std.dat", "wb+")) == NULL) {
             printf("\n Unable to Open std.dat File - exiting");
             getch();
             exit(1);
          }
       }
       do
       {  clrscr();
          printf("\n\nADMIN MENU:\n\n");
          printf("\n1 -  Add student record\n");
          printf("\n2 -  Display all entries\n");
          printf("\n3 -  Modify student record\n");
          printf("\n4 -  Delete student record\n");
    	   printf("\n5 -  Delete all entries\n");
          printf("\n6 -  Compress Records\n");
          printf("\n7 -  Sort Records\n");
    	   printf("\n8 -  Log-out and Exit\n");
          printf("\nEnter Choice: ");
          scanf("%d", &menu_choice);
          if(menu_choice==1) {add_record();}
          if(menu_choice==2) {disp_all();}
          if(menu_choice==3) {modifySR();}
          if(menu_choice==4) {delSR();}
    	   if(menu_choice==5) {delAll();}
          if(menu_choice==6) compress();
          if(menu_choice==7) sorter();
    	   if(menu_choice==8) {clrscr(); 
             printf("\n\n\nThank you for using SRMS! GoodBye!");}
       }while(menu_choice!=7);
    
       fclose(fp);
       getch();
       return 0;
    }
    /*8888888888888888888 Two New Functions 888888888888888888888888888888*/
    int getSize(void) {
      unsigned long length = 0;
      int size;
      fseek(fp, 0, SEEK_END);
      length = ftell(fp);
      size = length/sizeof(record);
      return size;
    }
    void sorter(void) {
       int getSize();
       int size, i, j, gar;
    
       record *studs, value;
       size = getSize();
       studs = malloc(size * sizeof(record));
    
       rewind(fp);
       printf("\n Our Current Records on File \n");
       for(i = 0; i < size; i++) {
          fread(&studs[i], sizeof(record), 1, fp);
          printf("\n%lld, %s %s %s", studs[i].snum, studs[i].lname, studs[i].fname, studs[i].course);
          
       }
       //now the records are in our studs[] array, ready to be sorted
    
       printf("\n\n");
       gar = getchar(); gar++;
       //this is Insertion sort
       for(i = 1; i < size; i++)  {
          value = studs[i];
          j = i - 1;
          while(j >= 0 && studs[j].snum > value.snum)  {
             studs[j + 1] = studs[j];
             --j;
          }
          studs[j+1] = value;
       }
       printf("\n Our sorted records in the array (but not yet on the file)");
       for(i = 0; i < size; i++) {
          printf("\n%lld, %s %s %s", studs[i].snum, studs[i].lname, studs[i].fname, studs[i].course);
       }
       /* if you want to write out the array to a new file, delete the old file,
       and rename the new file same as the old file name, do that here.
       */
       
       free(studs);
    }
    
    /***************************************************************************/
    
    void add_record() 
    {
       record stud;
       char ch;
       clrscr();
       fseek(fp,0,SEEK_END);
    
       gotoxy(35,1);printf("\n\nADD STUDENT RECORD\n\n");
    
       do
       {
          printf("\n\nStudent number (must be 4 digits) = ");
          scanf("%lld", &stud.snum);
          printf("\nPassword                          = ");
    	   scanf("%49s", stud.password);
          printf("\nLast name                         = ");
    	   scanf("%30s", stud.lname);
          printf("\nFirst name                        = ");
          scanf("%30s", stud.fname);
          printf("\nMiddle initial                    = ");
          scanf("%2s", stud.mi);
          printf("\nBirthdate (MMDDYY)                = ");
          scanf("%6s", stud.bday);
          printf("\nCourse (ex.BSM)                   = ");
          scanf("%5s", stud.course);
    
          printf("\n%lld, %s, %s, %s, %s, %s, %s", stud.snum, stud.password, 
          stud.lname, stud.fname, stud.mi, stud.bday, stud.course);
    
          fwrite(&stud,sizeof(stud),1,fp);
    
          printf("\n\nAdd another record? Y if yes\n\n");
          ch=toupper(getche());
    
       } while(ch=='Y');
       
       getch();
    }
    /***************************************************************************/
    void compress() {
       int i, rec_num = 0;
       FILE *fp1;
       record stud;
       rewind(fp);
       if((fp1=fopen("new.dat","wb")) == NULL) {
          printf("\n Unable to Open new.dat File - exiting");
          exit(1);
       }
       while(fread(&stud, sizeof(stud), 1, fp))  {
          if(stud.snum > 0) {
             fwrite(&stud, sizeof(stud), 1, fp1);
             rec_num++;
          }
       }
       fclose(fp);
       fclose(fp1);
       if((remove("std.dat")) != 0) {
          printf("\nError! Obsolete file: std.dat, was not deleted - exiting\n");
          exit(1);
       }
       else {
          rename("new.dat", "std.dat");
          if((fp=fopen("std.dat","rb+")) == NULL) {
             if((fp = fopen("std.dat", "wb+")) == NULL) {
                printf("\n Unable to Open std.dat File - exiting");
                getch();
                exit(1);
             }
          }
       }
       printf("\nThere are currently %d student records\n", rec_num);
       getch();
    }
    
    void disp_all() 
    {
       record stud;
       clrscr();
       rewind(fp);
       printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE  ");
    
       while(fread(&stud,sizeof(stud),1,fp)) {
          if(stud.snum > 0) {
          
             printf("%lld\t", stud.snum);
             printf("%-16s", stud.lname);
             printf("%-16s", stud.fname);
             printf("%-12s", stud.mi);
             printf("%-12s", stud.bday);
             printf("%-12s\n", stud.course);
          }
       }
       printf("\n\nPress any key to go back to menu.");
    
       getch();
    }
    /****************************************************************************/
    
    void modifySR() 
    {
       record stud;
       long long int snum;
       int x = 0;
       fpos_t filepos;
       clrscr();
       rewind(fp);
       gotoxy(35,2);printf("\n\nMODIFY STUDENT RECORD. Enter student to be modified.\n\n");
       printf("Enter student number    = ");
       scanf("%lld", &snum);
    
       while(fread(&stud,sizeof(stud),1,fp))
       {
          if(snum==stud.snum)
          {
             x++;
             printf("\n\n\n\nORIGINAL RECORD:\n");
             printf("\nStudent number    =  %lld", stud.snum);
             printf("\nPassword          =  %s", stud.password);
             printf("\nName              =  %s, %s %s", stud.lname, stud.fname, stud.mi);
             printf("\nBirthdate         =  %s", stud.bday);
             printf("\nCourse            =  %s", stud.course);
    
             printf("\n\n\n\nENTER NEW RECORD...\n");
             printf("\nStudent number    =  ");
             scanf("%lld", &stud.snum);
             printf("\nPassword          =  ");
             scanf("%49s", stud.password);
             printf("\nLast name         =  ");
             scanf("%30s", stud.lname);
             printf("\nFirst name        =  ");
             scanf("%30s", stud.fname);
             printf("\nMiddle initial    =  ");
             scanf("%2s", stud.mi);
             printf("\nBirthdate         =  ");
             scanf("%6s", stud.bday);
             printf("\nCourse            =  ");
             scanf("%5s", stud.course);
    
             fgetpos(fp, &filepos);
             filepos -= sizeof(stud);
             fsetpos(fp, &filepos);
    
             fwrite(&stud,sizeof(stud),1,fp);
             printf("\n\nRecord modified...");
             getch();
          }
       }
       if(x == 0)
          printf("\n\nNo record found...");
    
       getch();
    }
    /***************************************************************************/
    
    void delSR()
    
    {
       record stud;
       fpos_t filepos;
       int ch, j=0;
       long long int num;
       clrscr();
       rewind(fp);
       gotoxy(35,2);printf("\n\nDELETE STUDENT RECORD. Enter student to be deleted.");
       printf("\n\nStudent number   = ");
       scanf("%lld", &num);
    
       while( fread (&stud,sizeof(stud),1,fp) )
       {
          if(num==stud.snum)
          {
             j++;
             printf("\n\nStudent found: \n\n");
             printf("\nStudent number    =  %lld", stud.snum);
             printf("\nPassword          =  %s", stud.password);
             printf("\nName              =  %s, %s %s", stud.lname, stud.fname, stud.mi);
             printf("\nBirthdate         =  %s", stud.bday);
             printf("\nCourse            =  %s", stud.course);
    
    
             printf("\n\n\nAre you sure you want to delete the above student record(s)? Y if yes\n\n");
             ch=toupper(getche());
    
             if(ch=='Y')
             {
                filepos = ftell(fp);
                filepos -= sizeof(stud);
                fseek(fp,filepos,SEEK_SET);
                stud.snum = -1;  //indicating a deleted record
                fwrite(&stud,sizeof(stud),1,fp);
    
                printf("\n\nStudent record deleted... ");
             }
             else
                printf("\n\nNo Record Deleted...");
          }
       } 
       if(j == 0)
          printf("\n\nThat Record Was Not Found...");
       getch();
    
    }
    /****************************************************************************/
    
    void delAll()
    {
       record stud;
       char ch;
       clrscr();
       printf("\n\nAre you sure you want to delete all student records?\n\n");
       ch=toupper(getche());
    
       if(ch == 'Y')
       {
          fclose(fp);
          printf("\n All student records deleted");
          remove("std.dat");
       }
       else
          printf("Press any key to go back to menu.");
    
       getch();
    }
    EDIT: Adding the file handling to this would only be about 5 lines of code in sorter:

    1) remove(oldfilename)
    2) fopen(oldfilename, in "wb" mode) *yes, the same name as the one you just removed*
    3) use frwrite() to write out the data from the array, into the file.
    4) fclose(file pointer to oldfilename);
    Last edited by Adak; 04-22-2009 at 10:25 AM.

  3. #33
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    I apologize for what I said (in ref to MK's post). I said that because it was part of the instructions to merge the two together, or at least that's what I have understood.

    @Adak: *facepalm* God, how many debts do I owe you now? I'm sorry for being stupid. When I'm pressured, I really can't think anymore and if something goes wrong, my tendency is just to start over. We were not taught about all this to be honest (or maybe I was the one who skipped class or didn't listen), and I had no clue how to finish what I've started. I followed snippets. But some of them, I didn't know what variables they were talking about and I only got confused. I opted for tutorials but I found most of them unhelpful in the end. I can understand the concept of the procedure, but I didn't know how to use the predefined functions well enough, or at all. Some were easy, some were confusing. It's just different when someone's actually there explaining it to you. Perhaps if I did listen to my teacher's discussions, or to my mom's advice to buy a book, I would've understood better. I guess I was wrong for being complacent. I relied too much on myself even when I knew nothing. I haven't even touched a single book in programming. But enough of the rant, I should study your code again.

    Thanks again. Lesson learned.

  4. #34
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Adak: *facepalm* God, how many debts do I owe you now?
    You mean we're going to start counting - *now*?? "p

    Don't worry - nobody is born with programming skill. Sure, some people have a flair for it, like any other skill, but for 90% of us, it's just a matter of working at it, and learning from our mistakes, and working at it a bunch more.

    A great book for you is Ivor Horton's "Beginning C Programming". you'll get all the basics, and right up into the intermediate levels. The best thing, other than his easy to follow style of writing, is his examples of solving a problem, through programming. Those examples are a real gem, because he leads you through the solution, and the code to do that, step by step.

    The more you program, the better you'll get at it, no doubt. The reverse is also true - especially with C. You quit programming for 6 months, and you'll really fall back in your C skills.

    It's like a foreign language - you don't speak it, and pretty soon you're all "huh?" when you hear somebody speaking it fluently and quickly.

    I have a correction to make on the file handling at the end of the sorter() function. My comments above were close, but not exact, so here's the code for it:

    Code:
    /* this goes right after the print to screen of the sorted records, in sorter() */
    
       fclose(fp);
       i = 0;
    reopen:
    ;
       if((fp = fopen("tempstud", "wb")) == NULL) {
          printf("\n Error opening temp file in sorter()");
          i++;
          if(i < 3)  {
             sleep(1);
             goto reopen;
          }
          exit(1);
       }
    
       for(i = 0; i < size; i++)
          fwrite(&studs[i], sizeof(record), 1, fp);
    
    
       fclose(fp);
       remove("std.dat");
       rename("tempstud", "std.dat");    
       
       free(studs);
       if((fp = fopen("std.dat", "rb+")) == NULL) {
          printf("\n Error opening std.dat file, in sorter()");
          exit(1);
       }
    }  //end of the sorter function
    So get Horton's book, and get cracking!
    Last edited by Adak; 04-22-2009 at 06:36 PM.

  5. #35
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Uhm...there was a problem with the file handling...I'm doing it wrong again. See, I edited your code to follow instructions but I messed it up. I'm a failure!
    I mixed display and sort :|
    When I run it, it abnormally terminates if I choose display.

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    /****************************************************************************/
    
    typedef struct student
      {
      long long int snum;
      char password[50];
      char lname[31];
      char fname[31];
      char mi[3];
      char bday[7];
      char course[6];
      }record;
    
    /****************************************************************************/
    
    //DECLARATION OF MAJOR FUNCTIONS
    
    void add_record(void);
    void compress(void);
    void disp_all(void);
    void modifySR(void);
    void delSR(void);
    void delAll(void);
    void sorter(void);
    FILE* fp;
    
    /***************************************************************************/
    
    //MAIN
    
    int main()
    {
    	int menu_choice;
    	record stud;
    	if((fp=fopen("std.dat","rb+")) == NULL) {
    		if((fp = fopen("std.dat", "wb+")) == NULL) {
    			printf("\n Unable to Open std.dat File - exiting");
    			getch();
    			exit(1);
    		}
    	}
    	do
    	{  clrscr();
    		printf("\n\nADMIN MENU:\n\n");
    		printf("\n1 -  Add student record\n");
    		printf("\n2 -  Display all entries\n");
    		printf("\n3 -  Modify student record\n");
    		printf("\n4 -  Delete student record\n");
    		printf("\n5 -  Delete all entries\n");
    		printf("\n6 -  Log-out and Exit\n");
    		printf("\nEnter Choice: ");
    		scanf("%d", &menu_choice);
    		if(menu_choice==1) {add_record();}
    		if(menu_choice==2) {disp_all();}
    		if(menu_choice==3) {modifySR();}
    		if(menu_choice==4) {delSR();}
    		if(menu_choice==5) {delAll();}
    		if(menu_choice==6) {clrscr();
    			printf("\n\n\nThank you for using SRMS! GoodBye!");}
    	}while(menu_choice!=7);
    
    	fclose(fp);
    	getch();
    	return 0;
    }
    /***************************************************************************/
    int getSize()
    {
    	unsigned long length = 0;
    	int size;
    	fseek(fp, 0, SEEK_END);
    	length = ftell(fp);
    	size = length/sizeof(record);
    	return size;
    }
    
    /***************************************************************************/
    
    void add_record()
    {
    	record stud;
    	char ch;
    	clrscr();
    	fseek(fp,0,SEEK_END);
    
    	gotoxy(35,1);printf("\n\nADD STUDENT RECORD\n\n");
    
    	do
    	{
    		printf("\n\nStudent number (must be 9 digits) = ");
    		scanf("%lld", &stud.snum);
    		printf("\nPassword                          = ");
    		scanf("%49s", stud.password);
    		printf("\nLast name                         = ");
    		scanf("%30s", stud.lname);
    		printf("\nFirst name                        = ");
    		scanf("%30s", stud.fname);
    		printf("\nMiddle initial                    = ");
    		scanf("%2s", stud.mi);
    		printf("\nBirthdate (MMDDYY)                = ");
    		scanf("%6s", stud.bday);
    		printf("\nCourse (ex.BSM)                   = ");
    		scanf("%5s", stud.course);
    
    		printf("\n%lld, %s, %s, %s, %s, %s, %s", stud.snum, stud.password,
    		stud.lname, stud.fname, stud.mi, stud.bday, stud.course);
    
    		fwrite(&stud,sizeof(stud),1,fp);
    
    		printf("\n\nAdd another record? Y if yes\n\n");
    		ch=toupper(getche());
    
    	} while(ch=='Y');
    
    	getch();
    }
    /***************************************************************************/
    
    void disp_all()
    {
    	int size, x, j, gar, i, rec_num=0, getsize();
    	record stud;
    	record *studs, value;
    	FILE *fp1;
    
    	//count number of records
    
    	rewind(fp);
    	clrscr();
    	if((fp1=fopen("new.dat","wb")) == NULL)
    	{
    		printf("\n\nUnable to Open new.dat File - exiting\n\n");
    		exit(1);
    	}
    
    	while(fread(&stud, sizeof(stud), 1, fp))
    	{
    		if(stud.snum > 0)
    		{
    			fwrite(&stud, sizeof(stud), 1, fp1);
    			rec_num++;
    		}
    	}
    
    	fclose(fp);
    	fclose(fp1);
    	if((remove("std.dat")) != 0)
    	{
    		printf("\n\nError! Obsolete file: std.dat, was not deleted - exiting\n\n");
    		exit(1);
    	}
    	else
    	{
    		rename("new.dat", "std.dat");
    		if((fp=fopen("std.dat","rb+")) == NULL)
    		{
    			if((fp = fopen("std.dat", "wb+")) == NULL)
    			{
    				printf("\n\nUnable to Open std.dat File - exiting\n\n");
    				getch();
    				exit(1);
    			}
    		}
    	}
    
    	// sort records (insertion sort) and display
    
    	size = getSize();
    	studs = malloc(size * sizeof(record));
    
    	rewind(fp);
    	for(x = 0; x < size; x++)
    	{
    		fread(&studs[x], sizeof(record), 1, fp);
    
    	}
    	printf("\n\n");
    	gar = getchar(); gar++;
    	for(x = 1; x < size; x++)
    	{
    		value = studs[x];
    		j = x - 1;
    		while(j >= 0 && studs[j].snum > value.snum)
    		{
    			studs[j + 1] = studs[j];
    			--j;
    		}
    		studs[j+1] = value;
    	}
    	printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE  ");
    	for(x = 0; x < size; x++)
    	{
    		printf("%lld\t", studs[x].snum);
    		printf("%-16s", studs[x].lname);
    		printf("%-16s", studs[x].fname);
    		printf("%-12s", studs[x].mi);
    		printf("%-12s", studs[x].bday);
    		printf("%-12s\n", studs[x].course);
    
    	}
    
    	fclose(fp);
    	remove("std.dat");
    	rename("tempstud", "std.dat");
    
    	free(studs);
    	if((fp = fopen("std.dat", "rb+")) == NULL)
    	{
    		printf("\n Error opening std.dat file, in sorter()");
    		exit(1);
    	}
    	printf("\n\nThere are currently %d records.", rec_num);
    	printf("\n\nPress any key to go back to menu.");
    	getch();
    }
    /****************************************************************************/
    
    void modifySR()
    {
    	record stud;
    	long long int snum;
    	int x = 0;
    	fpos_t filepos;
    	clrscr();
    	rewind(fp);
    	gotoxy(35,2);printf("\n\nMODIFY STUDENT RECORD. Enter student to be modified.\n\n");
    	printf("Enter student number    = ");
    	scanf("%lld", &snum);
    
    	while(fread(&stud,sizeof(stud),1,fp))
    	{
    		if(snum==stud.snum)
    		{
    			x++;
    			printf("\n\n\n\nORIGINAL RECORD:\n");
    			printf("\nStudent number    =  %lld", stud.snum);
    			printf("\nPassword          =  %s", stud.password);
    			printf("\nName              =  %s, %s %s", stud.lname, stud.fname, stud.mi);
    			printf("\nBirthdate         =  %s", stud.bday);
    			printf("\nCourse            =  %s", stud.course);
    
    			printf("\n\n\n\nENTER NEW RECORD...\n");
    			printf("\nStudent number    =  ");
    			scanf("%lld", &stud.snum);
    			printf("\nPassword          =  ");
    			scanf("%49s", stud.password);
    			printf("\nLast name         =  ");
    			scanf("%30s", stud.lname);
    			printf("\nFirst name        =  ");
    			scanf("%30s", stud.fname);
    			printf("\nMiddle initial    =  ");
    			scanf("%2s", stud.mi);
    			printf("\nBirthdate         =  ");
    			scanf("%6s", stud.bday);
    			printf("\nCourse            =  ");
    			scanf("%5s", stud.course);
    
    			fgetpos(fp, &filepos);
    			filepos -= sizeof(stud);
    			fsetpos(fp, &filepos);
    
    			fwrite(&stud,sizeof(stud),1,fp);
    			printf("\n\nRecord modified...");
    			getch();
    		}
    	}
    	if(x == 0)
    		printf("\n\nNo record found...");
    
    	getch();
    }
    /***************************************************************************/
    
    void delSR()
    
    {
    	record stud;
    	fpos_t filepos;
    	int ch, j=0;
    	long long int num;
    	clrscr();
    	rewind(fp);
    	gotoxy(35,2);printf("\n\nDELETE STUDENT RECORD. Enter student to be deleted.");
    	printf("\n\nStudent number   = ");
    	scanf("%lld", &num);
    
    	while( fread (&stud,sizeof(stud),1,fp) )
    	{
    		if(num==stud.snum)
    		{
    			j++;
    			printf("\n\nStudent found: \n\n");
    			printf("\nStudent number    =  %lld", stud.snum);
    			printf("\nPassword          =  %s", stud.password);
    			printf("\nName              =  %s, %s %s", stud.lname, stud.fname, stud.mi);
    			printf("\nBirthdate         =  %s", stud.bday);
    			printf("\nCourse            =  %s", stud.course);
    
    
    			printf("\n\n\nAre you sure you want to delete the above student record(s)? Y if yes\n\n");
    			ch=toupper(getche());
    
    			if(ch=='Y')
    			{
    				filepos = ftell(fp);
    				filepos -= sizeof(stud);
    				fseek(fp,filepos,SEEK_SET);
    				stud.snum = -1;  //indicating a deleted record
    				fwrite(&stud,sizeof(stud),1,fp);
    
    				printf("\n\nStudent record deleted... ");
    			}
    			else
    				printf("\n\nNo Record Deleted...");
    		}
    	}
    	if(j == 0)
    		printf("\n\nThat Record Was Not Found...");
    	getch();
    
    }
    /****************************************************************************/
    
    void delAll()
    {
    	record stud;
    	char ch;
    	clrscr();
    	printf("\n\nAre you sure you want to delete all student records?\n\n");
    	ch=toupper(getche());
    
    	if(ch == 'Y')
    	{
    		fclose(fp);
    		printf("\n All student records deleted");
    		remove("std.dat");
    	}
    	else
    		printf("Press any key to go back to menu.");
    
    	getch();
    }
    Last edited by preeengles; 04-22-2009 at 07:19 PM.

  6. #36
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Oh, nevermind. I fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting problem?
    By audinue in forum C Programming
    Replies: 5
    Last Post: 01-06-2009, 02:16 PM
  2. Array Sorting problem
    By ___________ in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 12:17 AM
  3. What is problem about the sorting?
    By Mathsniper in forum C Programming
    Replies: 2
    Last Post: 04-17-2005, 07:00 AM
  4. Sorting text file problem...
    By John-m in forum C Programming
    Replies: 3
    Last Post: 10-01-2002, 04:51 PM
  5. Sorting array output problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 02-19-2002, 01:44 PM