Thread: replacing a file. delete old. rename new as old title.

  1. #1
    Registered User
    Join Date
    Jun 2017
    Posts
    88

    replacing a file. delete old. rename new as old title.

    I am working on a program that updates a sequential-access master file with data from a file containing recent transactions. After combining this data, a new master file is created. I have that much working so far, but what I want to do now is replace the old master file with the new master file. I want to do this by first deleting the old master file. Then I want to rename the new master file with the name of the old. I haven't used the rename function yet because I haven't gotten the remove function to work correctly. What happens is that the remove function returns 0, yet the file remains. I have been searching for answers on the internet, and it seems many people reviewing this issue have suggested it concerns the operating system. The operating system I am using is Windows 7 64 bit. The answers I have found suggest using the Windows API function GetLastError(). After running my code, GetLastError returns 183. This is the code for "Cannot create a file when that file already exists" so I'm not sure that this pertains to my issue.

    Code:
    #include <stdio.h>
    #include <windows.h>
     
    void createOld();
    void createTransfer();
    void createNew();
     
    int main(void) {
     
        createOld();
        createTransfer();
        createNew();
        
        if (remove("oldMaster.dat") != 0) {
            puts("Error deleting oldMaster.dat");
        }
        else {
            puts("oldMaster.dat file removed");
        }
        
        printf("error code: %d\n", GetLastError());
     
    }
     
    void createOld() {
        FILE* fPtr;
        if ((fPtr = fopen("oldMaster.dat", "w")) == NULL) {
            puts("Error opening oldMaster.dat for writing");
        }
     
        fprintf(fPtr, "%d %s %.2f\n", 100, "Alan Jones", (float)348.17);
        fprintf(fPtr, "%d %s %.2f\n", 300, "Mary Smith", (float)27.19);
        fprintf(fPtr, "%d %s %.2f\n", 500, "Sam Sharp", (float)0.00);
        fprintf(fPtr, "%d %s %.2f\n", 700, "Suzy Green", (float)-14.22);
     
        fclose(fPtr);
    }
     
    void createTransfer() {
        FILE* fPtr;
        if ((fPtr = fopen("transfer.dat", "w")) == NULL) {
            puts("Error opening transfer.dat for writing");
        }
     
        fprintf(fPtr, "%d %.2f\n", 100, 27.14);
        fprintf(fPtr, "%d %.2f\n", 300, 62.11);
        fprintf(fPtr, "%d %.2f\n", 400, 100.56);
        fprintf(fPtr, "%d %.2f\n", 900, 82.17);
     
        fclose(fPtr);
    }
     
    void createNew() {
        FILE* ofPtr;
        if ((ofPtr = fopen("oldMaster.dat", "r")) == NULL) {
            puts("Error opening oldMaster.dat!");
            return;
        }
     
        FILE* tfPtr;
        if ((tfPtr = fopen("transfer.dat", "r")) == NULL) {
            puts("Error opening transfer.dat!");
            return;
        }
     
        FILE* nfPtr;
        if ((nfPtr = fopen("newMaster.dat", "w")) == NULL) {
            puts("Error opening new master.dat!");
            return;
        }
     
        struct record {
            int accNum;
            char firstName[10];
            char lastName[10];
            float bal;
        } old;
     
        struct adjust {
            int accNum;
            float bal;
        } trans;
     
        fscanf(ofPtr, "%d %s %s %f", &old.accNum, old.firstName, old.lastName, &old.bal);
     
        while (!feof(ofPtr)) {
     
            fscanf(tfPtr, "%d %f", &trans.accNum, &trans.bal);
     
            while (old.accNum != trans.accNum && !feof(tfPtr)) {
     
                fscanf(tfPtr, "%d %f", &trans.accNum, &trans.bal);
            }
     
            if (old.accNum == trans.accNum) {
     
                fprintf(nfPtr, "%d %s %s %.2f\n", old.accNum, old.firstName, old.lastName, old.bal - trans.bal);
            }
            else {
     
                fprintf(nfPtr, "%d %s %s %.2f\n", old.accNum, old.firstName, old.lastName, old.bal);
            }
     
            fscanf(ofPtr, "%d %s %s %f", &old.accNum, old.firstName, old.lastName, &old.bal);
     
            rewind(tfPtr);
        }
        rewind(ofPtr);
     
        fscanf(tfPtr, "%d %f", &trans.accNum, &trans.bal);
     
        while (!feof(tfPtr)) {
     
            fscanf(ofPtr, "%d %s %s %f", &old.accNum, &old.firstName, &old.lastName, &old.bal);
     
            while (!feof(ofPtr) && old.accNum != trans.accNum) {
                fscanf(ofPtr, "%d %s %s %f", &old.accNum, &old.firstName, &old.lastName, &old.bal);
            }
     
            if (old.accNum != trans.accNum) {
     
                printf("Unmatched transaction record for account %d\n", trans.accNum);
            }
     
            fscanf(tfPtr, "%d %f", &trans.accNum, &trans.bal);
        }
     
        fclose(ofPtr);
        fclose(tfPtr);
        fclose(nfPtr);
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Your formats are off to begin with.
    Code:
    $ gcc foo.c
    foo.c: In function ‘createNew’:
    foo.c:110:23: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[10]’ [-Wformat=]
             fscanf(ofPtr, "%d %s %s %f", &old.accNum, &old.firstName, &old.lastName, &old.bal);
                           ^
    foo.c:110:23: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘char (*)[10]’ [-Wformat=]
    foo.c:113:27: warning: format ‘%s’ expects argument of type ‘char *’, but argument 4 has type ‘char (*)[10]’ [-Wformat=]
                 fscanf(ofPtr, "%d %s %s %f", &old.accNum, &old.firstName, &old.lastName, &old.bal);
                               ^
    foo.c:113:27: warning: format ‘%s’ expects argument of type ‘char *’, but argument 5 has type ‘char (*)[10]’ [-Wformat=]
    Other than that, it works for me.
    Code:
    $ gcc foo.c
    $ ls -l *.dat
    ls: cannot access '*.dat': No such file or directory
    $ ./a.out 
    Unmatched transaction record for account 400
    Unmatched transaction record for account 900
    oldMaster.dat file removed
    $ ls -l *.dat
    -rw-rw-r-- 1 sc sc 85 Oct 20 07:59 newMaster.dat
    -rw-rw-r-- 1 sc sc 41 Oct 20 07:59 transfer.dat
    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.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    I spent all day trying to figure this out and I just now realized that I was looking in the wrong directory. I had created a previous version of this program in another folder and was looking at the files that belonged to the previous version and wondered why oldMaster.dat was not deleting.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to move / rename a file in C.
    By Spork Schivago in forum C Programming
    Replies: 4
    Last Post: 02-11-2016, 12:38 PM
  2. Replies: 6
    Last Post: 01-26-2009, 08:01 PM
  3. Rename a file
    By Deb in forum C Programming
    Replies: 3
    Last Post: 04-16-2007, 02:43 PM
  4. Rename file problem
    By Emporio in forum C Programming
    Replies: 2
    Last Post: 06-05-2002, 09:36 AM
  5. rename a file on the C drive
    By cyberpuck52 in forum C Programming
    Replies: 3
    Last Post: 01-20-2002, 01:52 PM

Tags for this Thread