Rename Files In Dev C

Dev

  • The C Standard Library

Are these files in one folder? If so, you can get all files from this folder into a FileInfo (you need fileInfo, because you need a full path, so set files back where they were before renaiming). Then you do some additional code of renaiming and use Move method for an actual renaiming. For example, to rename the file file1.txt as file2.txt you would run: mv file1.txt file2.txt Renaming multiple files with the mv Command # The mv command can rename only one file at a time, but it can be used in conjunction with other commands such as find or inside bash for or while loops to rename multiple files. Dev-C A free, portable, fast and simple C/C IDE. Create new file in project rename 'untiled' file to 'foobar.h' save 'foobar.h' rename 'foobar.h' file 'FooBar.

  • C Standard Library Resources
  • C Programming Resources
  • Selected Reading

Description

The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.

Files

Rename Files In Dev C++

Declaration

Following is the declaration for remove() function.

Parameters

  • filename − This is the C string containing the name of the file to be deleted.

Return Value

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

Example

The following example shows the usage of remove() function.

Let us assume we have a text file file.txt having some content. So we are going to delete this file, using the above program. Let us compile and run the above program to produce the following message and the file will be deleted permanently.


In two previous tutorials we talked about file I/O functions on text files and file I/O functions on binary files. In this C programming language tutorial we look at how to remove files and how to rename a file.

Remove a File

Rename Files In Dev Code

With function remove() we can delete a file. Let us take a look at an example:

First a buffer is made with room for 100 characters, plus ‘0’. After the question “Name of file to delete:” is printed on the screen the gets_s is waiting for input of a maximum of 100 characters. (It is also possible to use gets(buffer) but gets_s is more secure, because you can’t overflow the buffer.)

In the “if statement” the function remove is used to remove the file. If the file is successfully deleted then a success message is printed. If something goes wrong (for example the file doesn’t exist) an error message is printed on the standard error stream.

Renaming a File

With the function rename() we can rename a file. Let us take a look at an example:

Rename files in dev command

As in the previous example a buffer for the filename is created of 100 characters, plus a character for ‘0’.
This time there is also a second buffer created that will hold the new name.

In the “if statement” the function rename() is used. The function rename() needs two variables, one for the old name and one for the new file name. If the renaming either succeeds or fails, a message is printed.

As you can see removing or renaming a file is easy!

Rename Files In Dev Code

That’s all for this C tutorial.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! or use to share this post with others.