C语言读取文件夹一级子目录的指定类型文件

参考链接:

C语言读取指定文件夹下的所有文件(各种信息)_c语言读取文件夹下的所有文件-CSDN博客 

参考以上文案,做成了几个函数,用于读取任意文件夹下的指定后缀的文件。

#include 
#include 
#include 
void loadfile_scanf(char *dirname,char* next,char* kind,char **msg)
{
	//文件存储信息结构体
	struct _finddata_t fileinfo;
	//保存文件句柄
	long fHandle;
	//文件数记录器
	char dirpath[100];
	strcpy(dirpath,dirname);
	if(access(dirpath,0)==-1)								// 检查文件夹是否存在,不存在为 -1
	{
		mkdir(dirpath);
		return;
	}
	else
	{
		int i = 0;																	// 记录文件数量
		char kindpath[100];
		strcpy(kindpath,dirpath);
		strcat(kindpath,next);
		strcat(kindpath,kind);
		if( (fHandle=_findfirst( kindpath, &fileinfo )) == -1L ) 					// *是通配符,默认在当前文件夹内查找文件,这里查找 .png 文件
		{
			printf( "当前目录: %s 下没有 txt 文件\n",dirname);
			return ;
		}
		else
		{
			FILE *fp;
			char test[100]="";														// 存储读取到的数据
			char path[100];
			do
			{
				i ++;
				printf( "找到文件:%s,文件大小:%d bit\n", fileinfo.name,fileinfo.size);
				strcpy(path,dirname);
				strcat(path,next);
				strcat(path,fileinfo.name);
				fp=fopen(path,"r");
				fscanf(fp,"%s\n",test);
				printf("已接收内容:%s\n",test);
				fclose(fp);
				strcpy(msg[i],test);
			}
			while( _findnext(fHandle,&fileinfo)==0);
		}
		//关闭文件
		_findclose( fHandle );

		printf("文件数量:%d\n",i);
	}
}
void showmsg(char** msg,int num)
{
	for(int i=1; i

你可能感兴趣的:(c语言,开发语言)