0

How do I got about renaming a file the file is called "Patient.txt" and i wish for it to be called whatever value is stored in the string "surname" after the string combine. I know my code is also riddled with small errors and bad practises which I will try and amend however im looking to fix the FILE name problem.

FILE*fopenFile(char fileName[], char fileMode[])
{
     int height, weight;
     char name[25], issues[30], curMeds[30], surname[25], birthDay[6];
     FILE *fbor = fopen(fileName, fileMode); 
     if(!fbor)
     {
        puts("Unable to open File");
        exit(1);
     }
     printf("Enter First name: ");
     scanf("%s", name);  
     printf("Enter Surname: ");
     scanf("%s", surname);  
     printf("Enter your DoB (ddmmyy): ");
     scanf("%s", birthDay);
     printf("Enter weight (Kg): ");
     scanf("%d", &weight);
     printf("Enter Height in CM: ");
     scanf("%d", &height);
     printf("Enter Medical Issues: ");
     fgets(issues, 30, stdin);
     emptyBuffer();
     printf("Enter Current Medication: ");
     fgets(curMeds, 30, stdin);
     
     fprintf(fbor, "First Name: %s\n", &name[25]);
     fprintf(fbor, "Surname: %s\n", &surname[25]);
     fprintf(fbor, "DoB: %s\nHeight: %d\n", &birthDay[6], height);
     fprintf(fbor, "Weight: %d\n", weight);
     fprintf(fbor, "Medical issues: ");
     fprintf(fbor, issues);
     fprintf(fbor, "Current medication: ");
     fprintf(fbor, curMeds);
     fclose(fbor);
     
     char fileType[] = ".mxa";
     
     strncat(surname, birthDay, 6);
     strncat(surname, fileType, 4);
     
     printf("\n  ..............................");
     printf("\n   File Saved as %s", surname);
     printf("\n  ..............................\n\n\n");
     
     if (rename(fileName, surname) == 0)
     {
        printf("File renamed successfully.\n");
     }
     else
     {
        printf("rename file failed.\n");
     }
     exit(0);
     
     return 0;
}

The files Name is not changing.

WillP
  • 1
  • 2
  • The number of incorrect string arguments to both `scanf` and `fprintf` in this code outnumber the correct ones; byt a *lot*. Just look at the first two `scanf` calls. One of those is (almost) correct. I.e. `rename` is the least of your worries right now. – WhozCraig Dec 17 '20 at 13:52
  • Why do you have a second `printf("%s\n", fileName);` after the call to `rename`? Do you expect it to print something else than the first `printf("%s\n", fileName);`? BTW your code is indented correctly... – Jabberwocky Dec 17 '20 at 13:54
  • All the code is running correctly though when run , just the file is being saved as the original name. – WillP Dec 17 '20 at 13:55
  • Please see [fgets() doesn't work after scanf](https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf), and [Removing trailing newline character from fgets() input](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) – Weather Vane Dec 17 '20 at 13:56
  • yeah the second printf("%s\n", fileName); is a test to see if the file has renamed. – WillP Dec 17 '20 at 13:56
  • `rename` will rename the _file_, but it won't magically change the content of `fileName`. BTW `rename` may fail and you don't check this. Read the [documentation of rename](http://www.cplusplus.com/reference/cstdio/rename/) closely – Jabberwocky Dec 17 '20 at 13:58
  • 1
    `&name[25]` etc are very wrong. That's the address of the next character *past* the buffer. The undefined behaviours may have corrupted `FILE *fbor` which has alreay been assigned. – Weather Vane Dec 17 '20 at 13:58
  • I'll amend the bad practices once i've figured this out. I've never actually programed before and this is my first assignment. I fclosed(fbor) below the last fprintf and its still not renaming and is coming up with the error used from the code solution above – WillP Dec 17 '20 at 14:25
  • If you call rename() two times in a row with the same arguments, be sure the second time will fail, unless oldname==newname (which should fail anyway? Never tried that!)... – linuxfan says Reinstate Monica Dec 17 '20 at 16:06
  • oh wow your're right. what a melt I am haha ill change that now , thankyou! – WillP Dec 17 '20 at 16:22
  • still not changing the file name. – WillP Dec 17 '20 at 16:23

2 Answers2

1

Function call:

int rename(const char * oldname, const char * newname);

program : Program to rename a file using rename() function

/**
 * C program to rename a file using rename() function.
 */

#include <stdio.h>


int main()
{
    // Path to old and new files
    char oldName[100], newName[100];

    // Input old and new file name
    printf("Enter old file path: ");
    scanf("%s", oldName);

    printf("Enter new file path: ");
    scanf("%s", newName);


    // rename old file with new name
    if (rename(oldName, newName) == 0)
    {
        printf("File renamed successfully.\n");
    }
    else
    {
        printf("Unable to rename files. Please check files exist and you have permissions to modify files.\n");
    }

    return 0;
}

Source

1

You must close the file before you are able to rename it, with the function rename. This seems to be your main issue within the question asked. Also, the arguments of "rename" function should be "const char*", which is not the case of the variables you are passing.

Note: There aresmall mistakes and bad practices on this core snipped, but since the question is not about it, I`m not commenting about it.

João PGC
  • 31
  • 3
  • 2
    *You must close the file before you are able to rename it, with the function `rename`.* That's basically a Windows-only restriction. POSIX systems in general not only allow a file to be renamed at any time, they even allow it to be deleted while open. It's a ***very*** useful paradigm. – Andrew Henle Dec 17 '20 at 14:11
  • I'll amend the bad practices once i've figured this out. I've never actually programed before and this is my first assignment. I fclosed(fbor) below the last fprintf and its still not renaming and is coming up with the error used from the code solution above. – WillP Dec 17 '20 at 14:16
  • "...the arguments of "rename" function should be "const char*", which is not the case of the variables you are passing" - that is not correct. `const char *` guarantees that **the rename function** will not use the pointer to whatever is pointed at. OP can pass an unqualified `char *` to `rename` and it will work Just Fine. – Bob Jarvis - Слава Україні Dec 17 '20 at 14:34
  • @BobJarvis-ReinstateMonica: Pass a pointer to a const-qualified type suggests that the called function will not modify the pointed-to object(s) through that pointer. It does not guarantee it. Because `const` was grafted onto C late in its development, there are situations where it is necessarily removed by a called function, and this is permitted and defined by the C standard if the pointed-to object was not originally defined with `const`. – Eric Postpischil Dec 17 '20 at 15:25