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

C语言 rename

admin4年前 (2020-11-26)代码相关2380

在<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# WPF程序 显示时间

C# WPF程序 显示时间

例子来源于此视频https://www.bilibili.com/video/av1422127?p=4 此例子是想说明有的类主要使用它的事件namespace Wpf_test {     /// <summary> &...

九九乘法表算法

九九乘法表算法

namespace _20200324 {     class Program     {         st...

C# 测量运行时间

使用Stopwatch类进行运行时间的监测要使用 System.Diagnostics 命名空间方法表 4Reset()停止时间间隔测量,并将运行时间重置为零。Restart()停止时间间隔测量,将运行时间重置为零,然后开始测量运行时间。Start()开始或继续测量某个时间间隔的运行时间。...

C# 返回值是接口的方法

今天写PIE二次开发加载栅格数据的时候发现类中方法的返回值是接口,之前没怎么写过,在此记录一下。在例子中设计一个接口 ICalculate ,接口中有两个方法, Add() 和 Div() 分别为加法和减法的功能,均有两个参数,参数和返回值的类型都是int类型。设计一个名为Calculat...

C语言malloc()函数

C语言中malloc()函数,用于分配所需的内存,并返回一个指向该内存的指针。注意这是C的标准库函数,不是C的关键字,在<stdlib.h>头文件下。函数声明: void *malloc(size_t size)其中,size是要分配的内存的大小,单位是字节。返回一个指针 ,指向已分配大...

C语言scanf一个容易出错的地方

今天用scanf()写一个数组循环输入,运行时很奇怪,明明只需要输入三个数,但是实际上要多输入一个,瞅了好一会才看到我是这么写的scanf("%d ",&p[i]);问题就出在这个 上,写printf()写习惯了,顺手就加上了 ,注意不要加!不要加!...