Thread: Something wrong with output

  1. #31
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Or wait a few hours for this:

    Lots of changes. In *very* limited testing everything works now. Added a compress() function to help make it more "industrial", and changed the way it deleted records, for the same reason.

    I used fsetpos() for one function, and fseek(), for file pointer moving, in another function. No particular reason.


    Code:
    #include <conio.h>
    #include <stdio.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);
    
    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 -  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) {clrscr(); 
             printf("\n\n\nThank you for using SRMS! GoodBye!");}
       }while(menu_choice!=7);
    
       fclose(fp);
       getch();
       return 0;
    }
    
    /***************************************************************************/
    
    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();
    }

  2. #32
    Registered User
    Join Date
    Apr 2009
    Posts
    33
    Wow, Adak, you're an angel. At first, I got really nervous cuz the first time I ran your code, it crashed. But ran it again and, YES! It worked! I'm so happy!

    Hmmm...I should learn more about file handling. I am not familiar with fsetpos()...Apparently, there's so much I have to learn. A long list of things to learn.

    Again, thank you sooooo much! I'm gonna study your code.

  3. #33
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It never crashed for me, but it was tested only briefly.

    One thing I noticed is that overflowing the fields is still a problem. I believe it's because the extra letters are still left on the keyboard buffer. That leaves the next field to accept them as *their* field, and then you're toast.

    To gain some control of that problem, you'd need to call a flush() type function, which would pull all the remaining char's off the keyboard buffer, between each field having data entered into them.

    Vart wrote one up maybe a week or two ago that looked quite capable, I'd search for that if you need any kind of strengthened input handling.

    I had a misadventure with this program, that was quite educational, if not funny. I had the file being opened in "append" mode. Worked fine, until I needed to move the file pointer away from the end of the file - the debug code showed me the file pointer had moved, but no, it did not. I was getting an inordinate amount of practice with cursing about that time. <mad!>

    Lesson learned - you can't move the file pointer away from the end of the file, if you open it in append mode. That's probably why they call it append mode, but it wasn't funny at the time.

    You're welcome, of course.

  4. #34
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop giving out fish, Adak.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #35
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, I hear ya, Quzah.

    With a program like this one, I could be making suggestions right through July.

    Now let's see, < pulls out "The Complete Guide to Teaching How to Fish" > . Rule number 1:

    "Don't give away fish, they'll never learn to fish for themselves."

    Check.

  6. #36
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Rule 2: If you must give fish away, poison it, so at least they won't come back asking for more.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong Output!
    By kolliash in forum C++ Programming
    Replies: 6
    Last Post: 06-19-2008, 07:55 AM
  2. Something Wrong with my function in Linux!
    By Matus in forum C Programming
    Replies: 5
    Last Post: 04-30-2008, 10:00 PM
  3. Getting wrong output from a class
    By orikon in forum C++ Programming
    Replies: 11
    Last Post: 11-18-2005, 07:58 PM
  4. Why is the output of this wrong?
    By Tokimasa in forum C++ Programming
    Replies: 4
    Last Post: 11-30-2004, 01:58 PM
  5. Leap year program prints wrong output
    By Guti14 in forum C Programming
    Replies: 8
    Last Post: 08-24-2004, 11:56 AM