当前位置:首页 > 代码相关 > 正文内容

C语言 rename

admin5年前 (2020-11-26)代码相关2924

在<stdio.h>头文件下

以下内容来自:http://www.cplusplus.com/reference/cstdio/rename/?kw=rename

rename

int rename ( const char * oldname, const char * newname );

Rename file

Changes the name of the file or directory specified by oldname to newname.

This is an operation performed directly on a file; No streams are involved in the operation.

If oldname and newname specify different paths and this is supported by the system, the file is moved to the new location.

If newname names an existing file, the function may either fail or override the existing file, depending on the specific system and library implementation.

Proper file access shall be available.

Parameters

  • oldname

    C string containing the name of an existing file to be renamed and/or moved. Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).

  • newname

    C string containing the new name for the file. Its value shall follow the file name specifications of the running environment and can include a path (if supported by the system).

Return value

If the file is successfully renamed, a zero value is returned. On failure, a nonzero value is returned. On most library implementations, the errno variable is also set to a system-specific error code on failure.

Example

/* rename example */
#include <stdio.h>

int main ()
{
  int result;
  char oldname[] ="oldname.txt";
  char newname[] ="newname.txt";
  result= rename( oldname , newname );
  if ( result == 0 )
    puts ( "File successfully renamed" );
  else
    perror( "Error renaming file" );
  return 0;
}

If the file oldname.txt could be successfully renamed to newname.txt the following message would be written to stdout:

File successfully renamed

Otherwise, a message similar to this will be written to stderr:

Error renaming file: Permission denied

rename

更改文件或路径的名字,由 oldname 改成 newname

这是直接在文件上执行的操作;操作中不涉及任何流。

如果 oldnamenewname 指定不同的路径,并且系统支持的话,则文件将移到新位置。

如果 newname 为已经存在的文件名字,则该函数可能会失败或覆盖现有文件,这取决于特定的系统和库实现。

应提供适当的文件访问。

参数

  • oldname

    包含要重命名和/或移动的现有文件的名称的字符串。它的值应该遵循运行环境的文件名规范,并且可以包括一个路径(如果系统支持的话)。

  • newname

    包含文件新名称的字符串。它的值应该遵循运行环境的文件名规范,并且可以包括一个路径(如果系统支持的话)。

返回值

如果成功重命名文件,则返回零值。如果失败,则返回一个非零值。在大多数库实现中,errno变量也被设置为失败时特定于系统的错误代码。

扫描二维码推送至手机访问。

版权声明:本文由lovedm.club发布,如需转载请注明出处。

本文链接:https://lovedm.club/?id=105

分享给朋友:

“C语言 rename” 的相关文章

C# 中左移运算

C# 中左移运算

将一个数换算成二进制,整体向左移动指定的位数。如:7的二进制在Int32中二进制表达为:00000000 00000000 00000000 00000111将其左移一位则变为:00000000 00000000 00000000 00001110若移位后超出最大位数,则超出部分舍掉,如:Int32...

C# 控制某句代码只执行一次

这两天用C#写了个2048游戏练手,在需求上如果最终达到了2048,那么应该给出一句提示或者弹出一个消息框,提示达到了2048,而且这个提示只需要展示一次,关闭提示后应该继续游戏而不会重复提示,可以使用bool类型的全局变量进行控制。如下:public partial class...

C# 正则表达式(1)

C# 正则表达式(1)

用于匹配输入文本的模式string s = "this is a test!"; string res = Regex.Replace(s, "^",&nbs...

C# 抽象类与接口的比较

相同:都不能被实例化都包含未实现的方法派生类必须实现未实现的方法不同:抽象类可以包含抽象成员,也可以包含非抽象成员,即抽象类可以是完全实现的,也可以是部分实现的,或者是完全不实现的。接口更像是只包含抽象成员的抽象类,或者说接口内的成员都是未被实现的。一个类只能继承一个抽象类(当然其它类也一样),但是...

PIE二次开发 加载栅格数据

1、获得栅格数据集路径2、打开栅格数据集3、创建栅格图层4、将数据添加到图层并刷新要添加两个引用:using PIE.DataSource;using PIE.Carto;// 获得要打开栅格数据的路径 OpenFileDialog file = new&n...

C、C++、C#交换变量

C、C++、C#交换变量

最近重新看了看C和C++,觉得有些地方挺有意思。作为一开始不管什么资料都会用来做例子的一个程序,交换变量。不管在哪,常用的int,float,double类型的变量都是值类型的,作为参数传到函数(方法)中时,其实是复制了一个值进去,也就是说通过函数是无法直接更改这些值的,只能通过一些间接的方法来更改...