C语言中 rename 的用法

C语言中 rename 的用法,第1张

rename函数功能是给一个文件重命名,用该函数可以实现文件移动功能,把一个文件的完整路径的盘符改一下就实现了这个文件的移动。具体参见下面的程序示例说明。

头文件:在Visual

C++6.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(argc

<

3)

{

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修改文件名:使用rename函数。

rename函数:功能描述: 改变文件的名称或者位置,如果目标已存在,将被自动覆盖。   用法:  #include <stdio.h>int rename(const char *oldpath, const char *newpath)参数:   

oldpath:旧文件名。 newpath:新文件名或者新位置。 

具体可以分以下2种情况:

1、修改单个文件

    直接使用rename即可。

2、批量修改文件(如:按一定规则修改某目录下所有文件)

    需要使用opendir遍历目录,然后修改该目录下文件。下面提供一个简单的例子。

void ModFilesName(const char *pcszPath)

{

    char szPathFile[1024] = {0}                            //路径+文件名

    DIR *dir_p

    struct dirent *direntp

    struct stat entryInfo

    //文件目录不存在,则创建

    if(stat(pcszPath, &entryInfo) < 0)

    {

        printf("Auto create folder:%s\n", pcszPath)

        mkdir(pcszPath, 0755)

    }

    

    if ((dir_p = opendir (pcszPath)) == NULL)

    {

        return

    }

    while ((direntp = readdir (dir_p)) != NULL)

    {

        //组合完整路径

        sprintf(szPathFile, "%s/%s", pcszPath, direntp->d_name)

        

        //判断文件是否是目录

        if(lstat(szPathFile, &entryInfo) == 0)

        {

            if(S_ISDIR(entryInfo.st_mode))

            {

                continue                                   //忽略目录

            }

            

            rename(szPathFile, 你要修改成的文件名)

        }

    } // while ( ...

    

    closedir (dir_p)

}

    

推荐一片文章:http://blog.chinaunix.net/uid-7525568-id-251530.html

希望能帮助到你,你的好评是我前进的动力!谢谢!

代码如下,自己琢磨下:(C-FREE3.5调试通过)#include <iostream>

#include <string>

#include <fstream>int main()

{

char filename[40]

char newfilename[40]//新名称

cout<<"请输入要新建的文件名:\n"

cin>>filename

ofstream file//创建个对象

file.open(filename)//创建文件

file.close() //关闭文件

char str[200]=""

strcat(str,"del ")

strcat(str,filename)

// system(str)//删除文件

char str1[200]=""

strcat(str1,"move ")

strcat(str1,filename)

strcat(str1," ")

cout<<"请输入新文件名:\n"

cin>>newfilename

strcat(str1,newfilename)

system(str1) //重命名文件}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11509597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存