Explain the methods of rename and remove of function in C language

  • 2020-04-02 03:18:02
  • OfStack

C rename() function: renames a file or directory
The header file:


#include <stdio.h>

The function rename() is used to rename a file, change a file path, or change a directory name
   


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

Oldname is the old file name, newname is the new file name.

Returns 0 if the file name is modified successfully, or -1 if not.

Rename file:

If the file specified by newname exists, it is deleted. If newname and oldname are not in the same directory, it is equivalent to moving the file.

Rename directory:

If both oldname and oldname are directories, rename the directory. If the directory specified by newname exists and is empty, the newname is first deleted. For both the newname and oldname directories, the calling process must have write permissions. When you rename a directory, newname cannot contain oldname as its path prefix. For example, you cannot rename /usr to /usr/foo/testdir, because the old name (/usr/foo) is a path prefix to the new name and therefore cannot be removed.

Example: a simple program to modify file names.


#include<stdio.h>
#include <fcntl.h>
int main(void)
{
  char oldname[100], newname[100];
  
  printf(" Please tell me the full path to a file : ");
  gets(oldname);
  printf(" You want to change to : ");
  gets(newname);
  
  if (rename(oldname, newname) == 0)
    printf(" The file has been sent  %s  Modified to  %s.n", oldname, newname);
  else
    perror("rename");
  return 0;
}

Operation results:


 Please tell me the full path of a file: test.ncb
 You want to change it to: test111.ncb
 The file has been sent test. ncb Modified to test111. ncb

The example first defines two arrays to store the file name specified by the user, then USES the function gets() to receive the file name entered by the user, then USES the function rename() to modify, and returns 0 on success.

Note: in practice, avoid using the gets() function as much as possible. Gets () can affect the security and robustness of your program

Another example is to design a program to modify files on the DOS command line.
Copy the new plain text window


#include <stdio.h>
void main(int argc, char **argv)
{
  if(argc < 3)
  {
    printf("Usage: %s old_name new_namen", argv[0]);
    return;
  }
  printf("%s=>%s", argc[1], argv[2]);
  if(rename(argv[1], argv[2]) < 0)
  printf("error!n");
  else
  printf("ok!n");
}

C remove() function: removes a file or directory
The header file:


#include <stdio.h>

The remove() function is used to delete the specified file, with the following prototype:


  int remove(char * filename);

Filename is the name of the file to be deleted. It can be a directory. If the parameter filename is a file, the unlink() processing is called. If the parameter filename is a directory, rmdir() is called to handle it.

[return value] returns 0 on success and -1 on failure. The reason for the error is in errno.

Error code:
EROFS   The file you want to write to is a read-only file.
EFAULT   The parameter filename pointer exceeds the accessible memory space.
ENAMETOOLONG   The parameter filename is too long.
ENOMEM   Out of core memory.
ELOOP   The parameter filename has too many symbolic joins.
EIO I/O   Access error.

The following program demonstrates how to use the remove() function to delete a file.


#include<stdio.h>
int main(){
  char filename[80];
  printf("The file to delete:");
  gets(filename);
  if( remove(filename) == 0 )
    printf("Removed %s.", filename);
  else
    perror("remove");
}

To run the program, first declare the character array variable to save the file name, get the file name from the console, then delete the file, and output the corresponding prompt based on the result of the deletion.

Note: in practice, avoid using the gets() function as much as possible. Gets () can affect the security and robustness of your program


Related articles: