-4

I am working on a inventory management project in c. In the project I need to delete records.

I have found out that to do this delete record work I have to search the specific record and write the other records to a temp file and the rename it to the original filename just after deleting the original file. But in my project code somehow the rename statement is skipping.

This is my project code:

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include<inttypes.h>
#include<windows.h>
struct record_stu
{
    unsigned long long int id;
    int gm;
    char subject[255],s[255],y[255];

};
main()
{
    struct record_stu st;
    FILE *fp,*fp_tmp;
    char c,subject[255],year[255],semester[255];
    unsigned long long int id;
    int gm,j,z;
    int records=0;
    const char* charString1 = "Student_ID";
    const char* charString2 = "Subject";
    const char* charString3 = "Year";
    const char* charString4 = "Semester";
    const char* charString5 = "Gained_Marks";
    const char* charString6 = "Yes";
    const char* charString7 = "No";
    //const char* charString8 = "CSE-4year1.dat";
    
    fp = fopen("CSE-4Year1.dat","r");
    if(!(fp))
    {
      printf("An Error has occurred.Department can't be Found\n");
    }
    else
    {
      printf("Department Found.\n");
      printf("Select a Specific Record to Delete!");
      printf("\nEnter Student ID\n");
      scanf("%"PRIu64"",&id);
      printf("\nEnter subject\n");
      scanf("%s",subject);
      printf("\nEnter Year\n");
      scanf("%s",year);
      printf("\nEnter Semester\n");
      scanf("%s",semester);
      printf("%s\t%s\t%s\t%s\t%s\n",charString1,charString2,charString3,charString4,charString5);
      rewind(fp);
      while (!feof(fp))
      {
        fscanf(fp,"%11"PRIu64"\t%3s\t%3s\t%3s\t%10d", &st.id, &st.subject, &st.y, &st.s,&st.gm);
        records++;
        fp_tmp = fopen("temp.dat","a");
        if(st.id==id)
        {
          if(strcmp(st.subject,subject)==0)
          {
            if(strcmp(st.y,year)==0)
            {
              if(strcmp(st.s,semester)==0)
              {
                printf("%11"PRIu64"\t%3s\t%3s\t%3s\t%10d\n", st.id,st.subject, st.y, st.s, st.gm);
                printf("Are Sure You Want This Record to be Deleted?(Yes(1)/No(2))\n");
                scanf("%d",&z);
                if(z==1)
                {
                  printf("%11"PRIu64"\t%3s\t%3s\t%3s\t%10d\n", st.id,st.subject, st.y, st.s, st.gm);
                }
                else if(z==2)
                {
                  break;
                }
                else
                {
                  printf("Wrong Input.Pleas check case of your input.\n");
                  break;
                }
              }
            }
          }
         }
         else
         {
           fprintf(fp_tmp,"%11"PRIu64"\t%3s\t%3s\t%3s\t%10d\n", st.id, st.subject, st.y, st.s,st.gm);
         }
        }
       }
       fclose(fp);
       fclose(fp_tmp);
       remove("CSE-4Year1.dat");
       if (rename("temp.dat","CSE-4Year1.dat") == 0)
       printf("Renamed successfully");
       else
       printf("Unable to rename the file");
       break;

The funny thing is I have a small code to rename that's working fine. Although the small code and project code has basically the same syntax.

The small code is here:

#include<stdio.h>
main()
{
 //remove("b.txt");
 rename("temp.dat","CSE-4Year1.dat");
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

Here is the complete code. The renaming problem is gone. The renaming problem occurs if the the file is open or doesn't exists in the directory.

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
#include<inttypes.h>
#include<windows.h>

//Variable Declarations
struct record_stu st;
FILE *fp,*fp_tmp,*fp1,*f,*f1;
unsigned long long int id;
char Selected_Exam[255],subject[255],Exam[255],year[255];
char semester[255],Dept_name[255],username[55],username_log[55], pass1[8]; 
char pass2[8],pasword[10],usrname[10],ch,pasword1[10];
char usrname1[10],Course_Code[255],ss[255],s1[255] = "";
char s2[255] = "",Gained_Letter_Grade[255],filename,lg,ct[255],cc[255],ct1[255];
int Course_Code_Number,i,gm,j=0,z,n=0,i,cs=0;
int flag=0,records=0,x[12],Dept_code,Gained_Mark,p=0;
float Gained_Grade_Point,Credit_Completed,gp,cd,tgp=0,tcd=0,cgpa=0,sgpa,pgp,ccr;

const char* charString1 = "Exam";
const char* charString2 = "Student_ID";
const char* charString3 = "Department Name";
const char* charString4 = "Course Code";
const char* charString5 = "Year";
const char* charString6 = "Semester";
const char* charString7 = "Letter Grade";
const char* charString8 = "Gained Mark";
const char* charString9 = "Grade Point";
const char* charString10 = "Credit Completed";
const char* charString11 = "Course Title";
const char* charString12 = "Yes";
const char* charString13 = "No";

struct record_stu
{
 unsigned long long int id;
 float sgpa;
 int gm;
 float gp,cd,ccr;
 char Exam[255],subject[255],semester[255],year[255];
 char Dept_name[255],Exam1[255],lg[255],ct[255],cc[255],ct1[255];
};
//Variable Declarations    
main()
{
 //File open command
 fp = fopen("Student - Copy.txt","r");
 fp_tmp = fopen("temp.txt","w");
 //File Open Command

 if(!(fp)) //File Open Error Detection
 {
   printf("An Error has occurred.File can't be Found\n");
 }
 else //Searching for the record to delete
 {
   fclose(fp_tmp);
   fclose(fp);
   printf("File Found.\n");
   rewind(fp);
   printf("\nEnter Student ID: ");
   scanf("%"PRIu64"",&id);
   printf("\nEnter Course Code:");
   scanf("%s",subject);

   fp = fopen("Student - Copy.txt","r");
   fp_tmp = fopen("temp.txt","w");
   while (fscanf(fp,"%s %"PRIu64"%s%s%s%s%s%d%f%f%s\n",st.Exam1,&st.id,st.Dept_name,st.subject,st.year,st.semester,&st.lg,&st.gm,&st.gp,&ccr,&st.ct1) != EOF)
      {
        records++;
        if(st.id==id && strcmp(st.subject,subject)==0)
        {
          printf("Do you want to delete the record?(Yes(1)/No(2))\nEnter Your Choice: ");
          scanf("%d",&z);
          printf("Confirmation Code = %d\n",z);
          if(z==1)
          {
             printf("Do you want to confirm deletion of the record?(Yes(1)/No(2))\nEnter Your Choice: ");
              scanf("%d",&p);
          }
        }
        else
        {
             fprintf(fp_tmp,"%s %11"PRIu64"\t%s\t%s\t%3s\t%3s\t%s\t%d\t%3.2f\t%f\t%s\n",st.Exam1,st.id,st.Dept_name,st.subject,st.year,st.semester,st.lg,st.gm,st.gp,ccr,st.ct1);
        }
        }


    }//Searching for the record to delete

    //Actual Delete operation by renaming the temporary file to the source file name
    if(p==1)
    {
        printf("Confirmation Code = %d\n",p);
        fclose(fp_tmp);
        fclose(fp);
        remove("Student - Copy.txt");
        if(rename("temp.txt","Student - Copy.txt")==0)
            printf("Delete Successful");
    }
    else
    {
        fclose(fp_tmp);
        fclose(fp);
        printf("Delete Unsuccessful");
        remove("temp.txt");
    }
    //Actual Delete operation by renaming the temporary file to the source file name
}