[英]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). 我想用C编写一个程序,该程序可以重命名MS Win 7环境(NTFS文件系统)下的许多文件。 Hence the
rename()
function seems to be a natural choice. 因此,
rename()
函数似乎是很自然的选择。 However, when I write the following in Dev-C++ (but naming the source file as .c): 但是,当我在Dev-C ++中编写以下内容时(但将源文件命名为.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. 我已经添加了
<stdio.h>
作为标题。 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;
}
You have declared a variable called rename
. 您已经声明了一个名为
rename
的变量。 This is confusing the compiler. 这使编译器感到困惑。
int i, rename;
I suggest you, erm, rename it. 我建议您,呃,重命名它。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.