Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change a text file's name in C++?

I would like to change a txt file's name, but I can not find how to do this.

For example, I want to rename foo.txt to boo.txt in my C++ program.

like image 765
Jail Avatar asked May 23 '11 21:05

Jail


People also ask

How do you change a file's name?

Open File Explorer by going to My Computer, or by pressing Windows Key + E on your keyboard. Find the file you want to rename, select it and select Rename on the ribbon (or press F2 on your keyboard). Type the new name you want the file to have and press Enter.

How do you rename a directory in C?

rename() function in Cint rename(const char * oldname, const char * newname); rename() function is defined in stdio. h header file. It renames a file or directory from oldname to newname .

How do you rename and delete a file in C?

The remove( ) function requires a C-style string as an argument. If an apstring type variable is used as an argument, you must engage the . c_str( ) to convert it to a standard C++ string variable. The rename( ) function takes the name of a file as its argument, and the new name of the file as a second argument.

How do you rename a text file in C++?

rename() Prototypeint rename( const char *oldname, const char *newname ); The rename() function takes a two arguments: oldname , newname and returns an integer value. It renames the file represented by the string pointed to by oldname to the string pointed to by newname . It is defined in <cstdio> header file.


1 Answers

#include <stdio.h> (or <cstdio>) and use rename (or std::rename):

rename("oldname.txt", "newname.txt");

Contrary to popular belief, this is included in the standard library, and is portable up to a point -- though of course the allowable contents of the strings will vary with the target system.

like image 99
Jerry Coffin Avatar answered Sep 21 '22 05:09

Jerry Coffin