Explain the rename () function and the use of the Remove () function in C language _c language

Source: Internet
Author: User

C language Rename () function: Renaming a file or directory
header file:

#include <stdio.h>

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

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

The parameter oldname is the old file name and NewName is the new file name.

"Return value" modifies the filename to return 0, otherwise return-1.

To rename a file:

    • If the file specified by NewName is present, it will be deleted.
    • If NewName and oldname are not in one directory, they are the equivalent of moving files.

To rename a directory:

    • If both Oldname and oldname are directories, rename the directory.
    • If the directory specified by newname exists and is an empty directory, the newname is deleted first.
    • For NewName and oldname two directories, the calling process must have write permissions.
    • When renaming 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 the path prefix of the new name and cannot be deleted.

"Instance" a simple program that modifies the filename.

#include <stdio.h>
#include <fcntl.h>
int main (void)
{
  char oldname[100], newname[100];
  /* Prompt for file to rename and new name *
  /printf ("Please tell me the full path of a file:");
  Gets (oldname);
  printf ("You want to modify to:");
  Gets (newname);
  /* Change filename */
  if (rename (oldname, newname) = 0)
    printf ("The file%s has been modified to%s.\n", Oldname, newname);
  else
    perror ("rename");
  return 0;
}

Run Result:

Please tell me the full path to a file: TEST.NCB
you want to change to: TEST111.NCB
has modified the file test. NCB to test111. NCB

The example first defines two arrays to store the user-specified file name, then uses the function gets () to receive the file name entered by the user, and then use the function rename () to modify, if successful then return a value of 0, prompt modification success.

Note: In actual development, avoid using the gets () function, gets () can affect the security and robustness of the program, see: C language gets () function: Reading a string from a stream

Another example is to design a program that modifies a file under a DOS command line.
Copy a plain text new window

#include <stdio.h>
void main (int argc, char **argv)
{
  if (argc < 3)
  {
    printf (Usage:%s old _name new_name\n ", 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 language Remove () function: Delete a file or directory
header file:

#include <stdio.h>

The Remove () function deletes the specified file in the following prototype:

  int remove (char * filename);

Parameter filename is the name of the file to be deleted and can be a directory. If the parameter filename is a file, call unlink () processing, or call rmdir () if the parameter filename is a directory.

"Return value" success returns 0, failure returns-1, the reason for the error is in errno.

Error code:
Erofs the file you want to write is a read-only file.
The efault parameter filename pointer exceeds the accessible memory space.
Enametoolong parameter filename is too long.
Enomem core memory is low.
Eloop parameter filename has too many symbolic connection problems.
Eio I/O access error.

The program Below "instances" 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 above program, first declare the character array variable that holds the file name, get the file name from the console, and then delete the file, and then output the corresponding message based on the result of the deletion.

Note: In actual development, avoid using the gets () function, gets () can affect the security and robustness of the program

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.