gpt4 book ai didi

c - 使用C代码重命名文件夹中的文件

转载 作者:行者123 更新时间:2023-11-30 20:01:30 25 4
gpt4 key购买 nike

我有 3 个文件夹,每个文件夹包含不同的文件名我想重命名每个文件夹的文件,如下例所示:

folder1                     folder 2                            folder3

image1.pnm image1.pnm image1.pnm

image2.pnm image2.pnm image2.pnm

..... ..... .....

imageN.pnm imageN.pnm imageN.pnm

void change_all_filename_in_dir(char *dirname)
{
char newname[] = "image";
char extension[] = ".jpg";
DIR *dir;
struct dirent *ent;
int i = 0;

if ((dir = opendir (dirname)) != NULL)
{
while ((ent = readdir (dir)) != NULL)
{
rename(ent->d_name, strcat(strcat(newname, i), extension);
i++;
}
closedir (dir);
}
else
{
printf(" folder not openend");
}
}

最佳答案

使用rename()函数。

#include <stdio.h>
int rename(const char *old, const char *new);

Description

The rename function causes the file whose name is the string pointed to by old to be henceforth known by the name given by the string pointed to by new. The file named old is no longer accessible by that name. If a file named by the string pointed to by new exists prior to the call to the rename function, the behavior is implementation-defined.

The rename function returns zero if the operation succeeds, nonzero if it fails, in which case if the file existed previously it is still known by its original name.

关于c - 使用C代码重命名文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com