如何判断文件夹中是否有重名的文件?

joe52 2007-03-22 01:33:36
请问如何用C语言,实现判断文件夹中是否有重名的文件?
我的重名是指不包括扩展名的文件名称

比如
00305.xml
00305.xml.zip

以上为重名的文件

如何判断呢??
谢谢

...全文
868 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu2020 2009-11-03
  • 打赏
  • 举报
回复
orc1984 2007-03-22
  • 打赏
  • 举报
回复
強 帮顶
jixingzhong 2007-03-22
  • 打赏
  • 举报
回复
比如第一个,
使用的链表结构,
对链表排序后,
比较相邻元素即可:

if(!strncpy(p->filename, p->next->filename, strlen(p->filename)))
printf("Find one filename: %s", p->filename);
p=p->next;
不想低调 2007-03-22
  • 打赏
  • 举报
回复
帮顶
jixingzhong 2007-03-22
  • 打赏
  • 举报
回复
利用链表实现目录内所有文件列表显示

#include <stdio.h>
#include <dirent.h>
/*#include <alloc.h>*/
#include <string.h>

void main(int argc,char *argv[])
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
if (argc!=2)
{
printf("Must specify a directory\n");
exit(1);
}
if ((directory_pointer=opendir(argv[1]))==NULL)
printf("Error opening %s\n",argv[1]);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node->next=(struct FileList *)malloc(sizeof(struct FileList));
node=node->next;
strcpy(node->filename,entry->d_name);
node->next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf("%s\n",node->filename);
node=node->next;
}
}
}


==================================================
==================================================

linux下面的,作者不是我

A Demo written by camelrain

/*
the program find a file from current directory or your defined directory
commond optinon [path] filename
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#define LENGTH 256

/* just if is a directory*/
static int IsDir (char * name);
/* search target file, arg1 is current path, arg2 is target file*/
static void search_file (char * path, char * name);

static int search_flag=0;


/*just if is a directory*/
static int IsDir (char * name) {
struct stat buff;

if (lstat(name,&buff)<0)
return 0; //if not exist name ,ignore

/*if is directory return 1 ,else return 0*/
return S_ISDIR(buff.st_mode);
}


/*search target file*/
static void search_file (char * path, char * name) {
DIR *directory;
struct dirent * dir_entry;
char buffer[LENGTH];

if ((directory=opendir(path)) == NULL) {
fprintf(stderr, "%s", path);
perror(" ");
return;
}


while (dir_entry=readdir(directory)) {
if (!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,"..")) {
/* do nothing*/
}
else {
/* if is boot directory add "/" */
if ((strcmp(path,"/"))==0)
sprintf(buffer,"%s%s",path,dir_entry->d_name);
/* if is not boot directory do not add "/"*/
else
sprintf(buffer,"%s/%s",path,dir_entry->d_name);

//printf("Now file : %s\n",buffer);
if (IsDir(buffer))
search_file (buffer , name);
else {
if (strcmp(dir_entry->d_name,name)==0)
{
printf("%s\n",buffer);
search_flag=1;
}
}
}
}
closedir(directory);
}

int main(int argc, char *argv[])
{
static char * current_dir;
static char * filename;
int length;

if (argc==1) {
printf("A few parameters!!\n");
return 0;
}

if (argc==2) {
current_dir=(char * )getcwd(current_dir,LENGTH);
filename=argv[1];
}

if (argc==3) {
length=strlen(argv[1]);

if (length>1 && (argv[1][length-1]=='/')) {
argv[1][length-1]='\0';
//printf("%d\n",strlen(argv[1]));
}

current_dir=argv[1];
filename=argv[2];
}

search_file(current_dir,filename);

if (!search_flag)
printf("Not found this(%s) file!\n",filename);

return 0;
}
jixingzhong 2007-03-22
  • 打赏
  • 举报
回复
根据文件名使用 strcmp 排序.......
根据规则,00305.xml.zip 和 00305.xml 相邻(假设中间没有类似文件名)
00305.xml 的后面的一个有序元素就是 00305.xml.zip
然后,
根据比较函数:
strncmp()
int strncmp(const char *s1,const char *s2,size_t maxlen)
比较字符串s1与s2中的前maxlen个字符

s1 s2为传入的两个有序元素,
maxlen为 00305.xml(前面的元素) 的长度

即可得到结果
hailongchang 2007-03-22
  • 打赏
  • 举报
回复
我的重名是指不包括扩展名的文件名称

比如
00305.xml
00305.xml.zip

---------------------------

00305.xml ------- 00305
00305.xml.zip ----- 00305.xml

不包括扩展名,它们一致吗?
intuition444 2007-03-22
  • 打赏
  • 举报
回复
把扩展名抛弃后,用strcmp()
joe52 2007-03-22
  • 打赏
  • 举报
回复
我个人觉得
先获取文件名的列表
然后通过文件名列表进行比较

但是有什么比较好的比较的算法呢?

70,005

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧