C++如何搜索出该程序目录下所有特定后缀名的文件?

C++如何搜索出该程序目录下所有特定后缀名的文件?,第1张

记录一个小技巧,查找一个目录下所有特定扩展名的的文件名。代码所示是查找所有后缀为“.txt”的文件名,并将结果存储于名为statfileurllist的vector中

注:也可以查找子目录

//存储文件名列表

vector<CString>statfileurllist

//查找TXT文件

WIN32_FIND_DATAA wfd

CString sPath ="*.txt"//查找指定目录下的所有格式的文件。

//CString sPath ="reso\\*.xls"

HANDLE hFile = FindFirstFile(sPath.GetBuffer(),&wfd)

if(INVALID_HANDLE_VALUE == hFile)

{

TRACE("不合法!\n")

return

}do{

statfileurllist.push_back(wfd.cFileName)

TRACE("%s\n",wfd.cFileName)

}while(FindNextFile(hFile,&wfd))

#include<stdio.h>#include<stdlib.h>//By cockhorseman QQ:1004828288void main(){ FILE *p char a[200],c,b,i if((p=fopen("zifu","r"))==0)//打开文件 {printf("文件打开失败!\n")}else{fscanf(p,"%s",a)printf("你要替换的字符\n")c=getchar()getchar()//结束接收第一个字符printf("你要替换成的字符\n")b=getchar() for(i=0a[i]!=0i++)//实现替换{if(a[i]==c)a[i]=b}printf("%s\n",a)fprintf(p,"%s",a)//文件输出fclose(p)} }

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

void file_replace(FILE *fp_in,FILE *fp_out,char *src,char *dst)

{

char c=fgetc(fp_in)

char tmp[100]

int count=0

while(c!=EOF)

{

int pPos=0

if(c!=src[pPos])

{

 fputc(c,fp_out)

 c=fgetc(fp_in)

 continue//如果当前字符不相等,继续下一个字符

}

memset(tmp,0,100)

while(c==src[pPos])

{

 tmp[pPos]=c

 c=fgetc(fp_in)

 pPos++//相等的部分跳过

}

if(src[pPos]=='\0')

{

 count++

 printf("源串已找到!替换%d处\n",count)

 fputs(dst,fp_out)

 continue

}

else

{

 fputs(tmp,fp_out)

 continue//如果当前字符不相等,继续下一个字符

}

}

return

}

int main()

{

char path[100],old_path[100]

printf("请输入文件路径:\n")

gets(path)

strcpy(old_path,path)

FILE *fp_in

FILE *fp_out

fp_in=fopen(path,"rt")

fp_out=fopen(strcat(path,".bak"),"wt")

if(!fp_in||!fp_out)

{

printf("File open error!")

}

char src[100],dst[100]

printf("请输入要替换的源串:\n")

gets(src)

printf("请输入要替换的目的串:\n")

gets(dst)

file_replace(fp_in,fp_out,src,dst)

fclose(fp_in)

fclose(fp_out)

char cmd[200]="copy "

strcat(cmd,path)

strcat(cmd," ")

strcat(cmd,old_path)

system(cmd)

memset(cmd,0,200)

strcat(cmd,"del ")

strcat(cmd,path)

system(cmd)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/tougao/11533214.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-16
下一篇2023-05-16

发表评论

登录后才能评论

评论列表(0条)

    保存