-1

below is my code.The code is use to update a text file

  1. copy all required data from "accountInformation.txt" to "temp.txt"
  2. delete the "accountInformation.txt" file
  3. rename the "temp.txt" file.

code is working well but only problem i am getting is remove function is not working for "accountInformation.txt". The remove function is working fine for other files.

Also rename function also not working if i am applying it on "accountInformation.txt"

by using errno.h on remove("accountInformation.txt"), its showing error no 13.

void updateRecord(int option)
{
    FILE *fp1;
    int i=1;
    FILE *fp2;
    searchRecord();
    if(option != 5)
    recordInput();

    char buffer[1000];  

    fp2 = fopen("temp.txt", "w");
    fp1 = fopen("accountInformation.txt","r");

    char str[256];
    while(!feof(fp1))
    {
        strcpy(str,"\0");
        fgets(str,256,fp1);
        if(!feof(fp1))
        {
            i++;
            if(i!=record)
            {
                fprintf(fp2,"%s",str);
            }
            else
            {
                if(option == 5)
                {
                     //do nothing
                }
                else
                {   
                    sprintf(buffer, "%d,%s,%s,%s,%s\n",i,accountNumber,firstName,lastName,accountBalance);
            fprintf(fp2,"%s",buffer);
                }
           }
        }
    }

if(fclose(fp1)!=0)
        fprintf(stderr, "Value of errno: %d\n", errno);

fclose(fp2);


   int rem = remove("C:/Users/umer/Desktop/New folder/accountInformation.txt");
   if(rem != 1)
    fprintf(stderr, "Value of errno: %d\n", errno);

rename("temp.txt","accountInformation.txt");



}
James Z
  • 12,209
  • 10
  • 24
  • 44

1 Answers1

0

If your file is open on your computer, a program cannot write or delete it. If you forgot you opened "accountInformation.txt", close it and try again.

melpomene
  • 84,125
  • 8
  • 85
  • 148
YlmzCmlttn
  • 3
  • 1
  • 3