Thread: Need help with database assignment!

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Need help with database assignment!

    When I go to delete something by using my delete function , i end up wiping the entire .DAT file. need help, can anyone tell me what im doing wrong


    Code:
    void delete_student()
    {
         another='Y';
         
           while(another=='Y'|| another=='y')
         {
              printf("\nEnter the id of the student to be deleted : \n");
              scanf("%s",&stud.id,student);
              buffer = (char *)malloc(1000*sizeof(char));
              memset(buffer,0,1000*sizeof(char));
              ptr = buffer;
              ft=fopen("TEMP.DAT","wb");
              
              if(ft !=NULL)
              {
                  while(!feof(fp))
                  {
                      fgets(stud.id,9,fp);
                      
                      if(strcmp(stud.id,"%s"),student !=0)
                        {
                            strcpy(ptr,stud.id);
                            ptr += strlen(stud.id);
                        }
              
                   fwrite(&stud,recsize,1,fp);         
                   fclose(fp);
                   fclose(ft);
                   remove("STUD.DAT");
                   rename("TEMP.DAT","STUD.DAT");
                   fp=fopen("STUD.DAT","a+b");
                   printf("Delete another Record(Y/N): \n");
                   fflush(stdin);
                   another=getchar();
                   ft=fopen("TEMP.DAT","w");
                   fprintf(fp,"%s",buffer);
                  } 
                  system("pause");
                      system("cls");
                         menu_function();
                   
               }    
                  
         }
    thats the first function I tried using a buffer.


    Code:
    void delete_student()
    {
         another='Y';
         
          
         
         while(another=='Y'|| another=='y')
         {
              printf("\nEnter the id of the employee to be deleted : \n");
              scanf("%s",&stud.id);
              ft=fopen("TEMP.DAT","wb");
              rewind(fp);
    
                   while(fread(&stud,recsize,1,fp)==1)
                   {
    
                      if(strcmp(stud.id,id)!=0)
    
                      fwrite(&stud,recsize,1,ft);
                   }
    
                   fclose(fp);
                   fclose(ft);
                   remove("STUD.DAT");
                   rename("TEMP.DAT","STUD.DAT");
                   fp=fopen("STUD.DAT","rb+");
                   printf("Delete another Record(Y/N): \n");
                   fflush(stdin);
                   another=getchar();
        }
        system("pause");
        system("cls");
       menu_function();
    }
    heres the other function

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    help is greatly appreciated and much needed. i will provide more of the code if needed? I have just spent weeks trying to sort this but no luck.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    anyone? :S

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    SourceForge.net: cpwiki
    For
    - indentation
    - abuse of feof()
    - abuse of fflush()

    Try renaming your fp and ft variables to say
    fReadingFromThisFile
    fWritingToThisFile

    and see how much sense some of your calls make.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    You need to look at the docs and some examples for strcmp

    This is not the correct way to compare stud.id to student.
    Code:
    if(strcmp(stud.id,"%s"),student !=0)

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Patience child. Your emergencies are not our emergencies. Besides, we volunteer our time, and most of us have jobs that supersede some stranger's homework assignment. Also, read the followin link: forum guidelines. Pay special attention to rule #3, where it says "Do not bump threads".

    You have a lot of problems. I am critiquing both pieces of code you posted in one section, since many of the errors overlap.

    1. Your indentation is crap. Read this: SourceForge.net: Indentation - cpwiki.
    2. You don't pass any parameters to your delete functions, which suggests you use global variables. Read this: Global Variables Are Bad. Declare all your variables in the appropriate functions and pass them as required.
    3. fflush(stdin) is undefined behavior, read this: FAQ > Why fflush(stdin) is wrong - Cprogramming.com. Here are some suggested alternatives: FAQ > Flush the input buffer - Cprogramming.com.
    4. Read this: FAQ > Why it's bad to use feof() to control a loop - Cprogramming.com.
    5. You don't check all your file operations for success. Every fopen, fgets, fwrite, etc must be checked to make sure it succeeds. E.g. if you can't open a file, no point in trying to read from or write to it.
    6. I have no idea what file fp refers to, or whether it was opened successfully and with the right mode, thus I can't tell if you're using it correctly. This goes along with the global variable comment above.
    7. You use fgets (which is for text files) to read from TEMP.DAT, then you use fwrite (which is for binary files) to write to the same file. This is almost certainly not what you want.
    8. Presumably, STUD.DAT is the actual database, and TEMP.DAT is where you write the new one, that has the specified student removed. Yet I never see you open STUD.DAT or read anything from it. You only read from TEMP.DAT. This segues into the next point
    9. You open TEMP.DAT in "w" mode. Read the fopen documentation, and see why TEMP.DAT is empty.
    10. In your buffer version, you: 1. malloc 1000 bytes, 2. set them all to 0, 3. set the useless ptr = buffer, 4. try to print buffer to fp as a string, but it's zero length, so nothing prints. All that, and to top it off, you don't free buffer, creating a memory leak.
    11. You call menu_program from your delete_student. Presumably, menu_program also calls delete_student, either directly or indirectly. This is called "mutual" or "indirect" recursion. If people use your program long enough, it will run out of stack space and just crash. Use loops instead if you want your program to repeat.
    12. You use OS-specific operations like system("pause") and system("cls"). There is no need for that, it usually makes your program awkward to use, with the screen resetting all the time, and it makes it difficult for non-Windows people to help you, since "pause" and "cls" are not valid commands on other OSes.



    There is almost certainly more stuff that needs fixing, but that's all my brain could catch on it's first pass.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    thanks for your reply, sorry about the bumping. I'll take some time to read through your points before reposting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 04-26-2009, 08:54 AM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Database to Database Transfer?
    By draggy in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2007, 10:50 AM
  4. database....
    By nomi in forum C Programming
    Replies: 9
    Last Post: 01-11-2004, 01:01 PM
  5. Replies: 1
    Last Post: 10-09-2001, 10:20 PM

Tags for this Thread