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
}
1
solved Basic rename statements’s unusal behaviour in c