重命名文件函数c语言 c语言重命名文件夹

C语言rename函数用法?rename函数功能是给一个文件重命名,用该函数可以实现文件移动功能 , 把一个文件的完整路径的盘符改一下就实现了这个文件的移动 。具体参见下面的程序示例说明 。
头文件:在Visual C6.0中用stdio.h或者io.h
用 法: int rename(char *oldname, char *newname);
程序例:
#include
int main(void)
{
char oldname[80], newname[80];
/* prompt for file to rename and new name */
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);
/* Rename the file */
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.\n", oldname, newname);
else
perror("rename");
return 0;
}
执行过程:
File to rename: D:\\in.dat
New name: G:\\in.dat
Renamed D:\\in.dat to G:\\in.dat.
这样就实现了in.dat从D盘移动到G盘 。
在unix或linux系统中:
#include
int rename(const char *oldname, const char *newname);
函数说明
(1) 如果oldname为一个文件而不是目录,那么为该文件更名 。在这种情况下,如果newname作为一个目录已存在,则它不能重命名一个目录 。如果newname已存在,而且不是一个目录,则先将其删除然后将oldname更名为newname 。对oldname所在目录以及newname所在的目录,调用进程必须具有写许可权 , 因为将更改这两个目录 。
(2) 如若oldname为一个目录 , 那么为该目录更名 。如果newname已存在,则它必须是一个目录,而且该目录应当是空目录(空目录指的是该目录中只有. 和.. 项) 。如果newname存在(而且是一个空目录),则先将其删除,然后将oldname更名为newname 。另外,当为一个目录更名时 , newname不能包含oldname作为其路径前缀 。例如,不能将/usr更名为/usr/foo/testdir , 因为老名字( /usr/foo)是新名字的路径前缀,因而不能将其删除 。
(3) 作为一个特例 , 如果oldname和newname引用同一文件,则函数不做任何更改而成功返回 。
返回值 执行成功则返回0,失败返回-1 , 错误原因存于errno
范例
#include
int main(int argc,char **argv)
{
if(argc3)
{
printf("Usage: %s old_name new_name\n",argv[0]);
return -1;
}
printf("%s = %s\n", argv[1], argv[2]);
if(rename(argv[1], argv[2])0 )
printf("error!\n");
else
printf("ok!\n");
return 0;
【重命名文件函数c语言 c语言重命名文件夹】}
c语言中 如何删除文件和重命名文件 , 举个例子可以么函数名: rename
功能: 重命名文件
用法: int rename(char *oldname, char *newname);
程序例:
#include stdio.h
int main(void)
{
char oldname[80], newname[80];
/* prompt for file to rename and new name */
printf("File to rename: ");
gets(oldname);
printf("New name: ");
gets(newname);
/* Rename the file */
if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.\n", oldname, newname);
else
perror("rename");
return 0;
}
--------------------------------------------------------
函数名: remove
功能: 删除一个文件
用法: int remove(char *filename);
程序例:
#include stdio.h
int main(void)
{
char file[80];
/* prompt for file name to delete */
printf("File to delete: ");
gets(file);
/* delete the file */
if (remove(file) == 0)
printf("Removed %s.\n",file);
else
perror("remove");
return 0;
}
--------------------------------------------------------
拷贝的话 ,
要么字节写一个函数;
或者使用 win API: CopyFile
--------------------------------------------------------
--------------------------------------------------------
/* Chapter 1. Basic cp file copy program. C library Implementation. */
/* cp file1 file2: Copy file1 to file2. */
#include windows.h
#include stdio.h
#include errno.h
#define BUF_SIZE 256
int main (int argc, char *argv [])
{
FILE *in_file, *out_file;
char rec [BUF_SIZE];
size_t bytes_in, bytes_out;
if (argc != 3) {
printf ("Usage: cp file1 file2\n");
return 1;
}
in_file = fopen (argv [1], "rb");
if (in_file == NULL) {
perror (argv [1]);
return 2;
}
out_file = fopen (argv [2], "wb");
if (out_file == NULL) {
perror (argv [2]);
return 3;
}
/* Process the input file a record at a time. */
while ((bytes_in = fread (rec, 1, BUF_SIZE, in_file))0) {
bytes_out = fwrite (rec, 1, bytes_in, out_file);
if (bytes_out != bytes_in) {
perror ("Fatal write error.");
return 4;
}
}
fclose (in_file);
fclose (out_file);
return 0;
}
用C语言编程修改文件名(C也行)修改文件名,可要调用操作系统提供的API函数,比如Windows上的MoveFile(),也可以直接调用cmd中已提供的重命名命令——rename 。下面的示例代码,调用rename命令来重名命文件名 。
#include stdio.h
#include stdlib.h
#include string.h
int main(int ac, char *pav[])
{
if (ac!=3) {
printf("程序名 要重命名的文件路径 新的文件名\n");
printf("示例:test.exe 1.txt 2.txt\n");
return 0;
}
if (access(pav[1], 0) !=0) {
printf("不存在该文件\n");
return 0;
}
char szcmd[256] = "cmd /c rename ";
strcat(szcmd, pav[1] );
strcat(szcmd, " ");
strcat(szcmd, pav[2]);
system(szcmd);
return 0;
}
关于重命名文件函数c语言和c语言重命名文件夹的介绍到此就结束了 , 不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站 。

    推荐阅读