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

C语言 rename

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

在<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://www.lovedm.club/?id=105

分享给朋友:

“C语言 rename” 的相关文章

递归计算1到x的和

递归真是个神奇的东西,当时学C的时候就没搞明白,学C#又遇到例子了。        public int SumXTo1(int x)     &n...

C# 属性,get,set使用

属性反映了状态,是对字段的自然扩展。下面的代码,有一个学生类,类中有年龄属性,通过get,set对年龄进行值的获取与设置,同时对年龄进行约束,使值控制在0-120之间,否则抛出异常信息。namespace _20200324_2 {     cl...

C#事件_Sample_1

事件模型的五个组成部分:1、事件的拥有者(event source,只能是对象或类)2、事件成员(event,成员)3、事件的响应者(event subscriber,对象)4、事件处理器(event handler,成员)--本质上是一个回调方法5、事件订阅--把事件...

C# Lambda表达式

简单用法,一句一句来,便于理解 Func<int, int, int> func = new Func<int, int, int>((int a, int b) => { return a * b; });(int a, int b) => { ret...

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

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

C# 使用FileStream进行文件复制操作

使用文件流进行操作,如下,其中注释部分是和非注释部分一样的,但是使用using语句是执行完后自动释放内存,而注释部分是手动释放。CopyFile方法中,缓冲区大小设为1024*1024字节,也就是1MB,Read方法和Write方法中,第一个参数都是缓冲区数组,第二个参数都是偏移量,这个量是在缓冲区...