怎样检查当前目录中的文件与文件列表中文件是否相符

ccjsj1 2010-12-02 09:52:25
当前文件与文件列表中文件不同时怎样输出缺少的文件,即
t5-20101103.txt
t6-20101103.txt


当前目录:mydir

当前目录的文件:(t1-t6是固定的,但是年月日是每天生成的,每天是相同的。)
t1-20101103.txt
t2-20101103.txt
t3-20101103.txt
t4-20101103.txt

文件列表文件:myfile
myfile文件内容如下:
t1
t2
t3
t4
t5
t6
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ccjsj1 2011-12-21
  • 打赏
  • 举报
回复
谢谢各位答复,问题已解决。

思路是,打开文件(fopen),生成当天时间,拼成新文件名;再打开目录(opendir),每次用拼成新文件与目录中文件比较一遍,打印不同的。
ccjsj1 2010-12-08
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 zhao4zhong1 的回复:]
没超过100行的代码也叫长?
那100000行的代码不就捅破宇宙了?
(^_^)
[/Quote]

帮解释解释吧,看不懂啊?
赵4老师 2010-12-02
  • 打赏
  • 举报
回复
仅供参考
//输出PROG中有但LIST中没有的文本行,即集合PROG-LIST
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <search.h>
#define MAXLINES 1000000
#define MAXCHARS 256
char buf[MAXLINES][MAXCHARS];
char P[256]="PROG";//程序Program需要的文件列表
char L[256]="LIST";//dir /b /s生成的实际文件列表List
FILE *fp,*fl;
int n,L1;
int ignore_case=0;
char ln[MAXCHARS];
int icompare(const void *arg1,const void *arg2) {
return stricmp((char *)arg1,(char *)arg2);
}
int compare(const void *arg1,const void *arg2) {
return strcmp((char *)arg1,(char *)arg2);
}
void main(int argc,char **argv) {
if (argc>1) strcpy(P,argv[1]);//命令行参数1覆盖PROG
if (argc>2) strcpy(L,argv[2]);//命令行参数2覆盖LIST
if (argc>3) ignore_case=1;//若存在命令行参数3,忽略大小写
if ((fl=fopen(L,"rt"))==NULL) {
printf("Can not open %s\n",L);
return;
}
if ((fp=fopen(P,"rt"))==NULL) {
fclose(fl);
printf("Can not open %s\n",P);
return;
}
n=0;
while (1) {
if (fgets(ln,MAXCHARS,fl)==NULL) break;//
L1=strlen(ln)-1;
while (1) {
if ('\n'==ln[L1]||' '==ln[L1]) {
ln[L1]=0;
L1--;
} else break;//
}
if (L1>=0) {
strcpy(buf[n],ln);
n++;
if (n>=MAXLINES) {
fclose(fl);
fclose(fp);
printf("%s up to %d lines",L,MAXLINES);
return;
}
}
}
fclose(fl);
if (ignore_case) qsort(buf,n,MAXCHARS,icompare);
else qsort(buf,n,MAXCHARS,compare);
while (1) {
if (fgets(ln,MAXCHARS,fp)==NULL) break;//
L1=strlen(ln)-1;
while (1) {
if ('\n'==ln[L1]||' '==ln[L1]) {
ln[L1]=0;
L1--;
} else break;//
}
if (L1>=0) {
if (ignore_case) {
if (NULL==bsearch(ln,buf,n,MAXCHARS,icompare)) printf("%s\n",ln);
} else {
if (NULL==bsearch(ln,buf,n,MAXCHARS,compare)) printf("%s\n",ln);
}
}
}
fclose(fp);
}
bdmh 2010-12-02
  • 打赏
  • 举报
回复
myfile为列表,然后查找文件时,取得前两个字符,在列表中查找,查到一个可以在列表中删掉一个,剩下的就是没有的
赵4老师 2010-12-02
  • 打赏
  • 举报
回复
没超过100行的代码也叫长?
那100000行的代码不就捅破宇宙了?
(^_^)
ccjsj1 2010-12-02
  • 打赏
  • 举报
回复
谢谢两位,楼上的太长了,能把思路说一下吗?

69,373

社区成员

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

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