LaBird
LaBird

Reputation: 299

rename() not working in a C program compiled using Dev C++

I would like to write a program in C that can rename a number of files under MS Win 7 environment (NTFS file system). Hence the rename() function seems to be a natural choice. However, when I write the following in Dev-C++ (but naming the source file as .c):

rename(name1, name2);

Compiling the file gives me the error:

[Error] called object 'rename' is not a function

I have already added <stdio.h> as the header. Is there anything I am missing?

The source code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 50
#define MAX 49

int main()
{
    int *p;
    int i, rename;
    char name1[25], name2[25];

    srand(time(NULL));
    rename = 0;

    p = (int *)malloc(sizeof(int) * N);
    for (i = 0; i < N; i++)
       p[i] = 0;

    for (i = 0; i < MAX; i++) {
        if (p[i] == 0) {
            printf ("** ", i);
            sprintf(name1, "abc%d.jpg", i);
            sprintf(name2, "abct.jpg");
            rename(name1, name2);
        }
    }
    return 0;

}

Upvotes: 0

Views: 1373

Answers (3)

65656565656
65656565656

Reputation: 91

You can try this.

#include <stdio.h>

int main(void)
{
    char filename[101], newfilename[101];

    printf("Please type in name of file to be changed : \n");
    scanf("%s", &filename); /* Get the filename from the keyboard) */

    printf("You have choosen to rename %s.\nPlease type the new name of the file : \n", filename);
    scanf("%s", &newfilename); /* Get the new filename from the keyboard) */

    if (rename(filename, newfilename) == 0)
    {
        printf("%s has been renamed to %s.\n", filename, newfilename);
    }
    else
    {
        fprintf(stderr, "Error renaming %s.\n", filename);
    }
    system("pause");
    system("CLS");

    return 0;
}

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60711

You have declared a variable called rename. This is confusing the compiler.

int i, rename;

I suggest you, erm, rename it.

Upvotes: 4

D3Hunter
D3Hunter

Reputation: 1349

I think you have some variables named with rename, that's why compiler gives called object 'rename' is not a function. Check this one. As said in the comment, please give more about your code.

Upvotes: 5

Related Questions