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.
Please help asap.
The small code is here
#include<stdio.h>
main()
{
//remove("b.txt");
rename("temp.dat","CSE-4Year1.dat");
}