How to modify the source file name in C language programming

C Modify the file name: use the rename function.

rename function: Function description: Change the name or location of the file. If the target already exists, it will be automatically overwritten. Usage: ?#include?lt; stdio.hgt; int?rename(const?char?*oldpath, ?const?char?*newpath); Parameters:

oldpath: old file name. ?newpath: New file name or new location. ?

It can be divided into the following two situations:

1. Modify a single file

Just use rename directly.

2. Modify files in batches (such as modifying all files in a directory according to certain rules)

You need to use opendir to traverse the directory and then modify the files in the directory. A simple example is provided below.

void?ModFilesName(const?char?*pcszPath)

{

char?szPathFile[1024]?=?{0}; //Path file Name

DIR?*dir_p;

struct?dirent?*direntp;

struct?stat?entryInfo;

//File If the directory does not exist, create it

if(stat(pcszPath,?amp;entryInfo)?lt;?0)

{

printf("Auto? create?folder:s\n",?pcszPath);

mkdir(pcszPath,?0755);

}

if?((dir_p?= ?opendir?(pcszPath))?==?NULL)

{

return;

}

while?((direntp ?=?readdir?(dir_p))?=?NULL)

{

//Combined full path

sprintf(szPathFile,?"s/ s",?pcszPath,?direntp-gt;d_name);

//Determine whether the file is a directory

if(lstat(szPathFile,?amp;entryInfo)?==? 0)

{

if(S_ISDIR(entryInfo.st_mode))

{

continue; //Ignore directory

}

rename(szPathFile,?The file name you want to modify);

}

}?//?while?(? ...

closedir?(dir_p);

}

Recommend an article: /uid-7525568-id-251530.html

I hope it can help you, your praise is my motivation to move forward!