【问题标题】:rename() not working in a C program compiled using Dev C++rename() 在使用 Dev C++ 编译的 C 程序中不起作用
【发布时间】:2014-12-28 14:26:44
【问题描述】:

我想用 C 语言编写一个程序,可以在 MS Win 7 环境(NTFS 文件系统)下重命名多个文件。因此rename() 函数似乎是一个自然的选择。但是,当我在 Dev-C++ 中编写以下内容时(但将源文件命名为 .c):

rename(name1, name2);

编译文件给我错误:

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

我已经添加了<stdio.h> 作为标题。我有什么遗漏吗?

源码如下:

#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;

}

【问题讨论】:

  • 这是 complete 错误吗?您是否包含了正确的头文件?
  • 请显示您的完整代码。然后我们可以说更多。
  • 您希望我们通过 clairvoyance-over-tcp/ip 修复您的错误?发布您的代码!
  • 假设原始文件名存在(您可以使用 stat() 来确定该详细信息)。然后使用类似: char buffer[100] = {'\0'}; sprintf(buffer, "重命名 %s %s\n", oldName, newName);系统(缓冲区);假设您的操作系统使用“重命名”来更改文件名,并假设您的程序有权更改特定文件的名称。

标签: c rename ntfs file-rename dev-c++


【解决方案1】:

我认为您有一些以rename 命名的变量,这就是编译器给出called object 'rename' is not a function 的原因。检查this one。如评论中所述,请提供有关您的代码的更多信息。

【讨论】:

  • 这确实是问题所在,现在我们已经看到了代码。
  • 是的,你是对的。我使用了一个名为rename 的变量。非常感谢!
【解决方案2】:

您已经声明了一个名为rename 的变量。这让编译器感到困惑。

int i, rename;

我建议你,呃,重命名它。

【讨论】:

    【解决方案3】:

    你可以试试这个。

    #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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-29
      • 2023-03-31
      • 2013-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多