C语言遍历文件夹内容 whl

1、函数介绍

func.h
/* 
*  文件名称:filename.h 
*  文件标识:见配置管理计划书 
*  摘     要:遍历某文件加下%i_%i_%i_%i_%i.txt格式的文件,存储到数组中
* 
*  当前版本:1.0
*  作     者:whl 
*  完成日期:2012-11-17
*/ 

#include "func.h"
#include <string.h>
#include <stdio.h>
#include <IO.H>

//将文件名转换为整形的以便建树
void convert(struct FileName_type txtfile[],int length)
{
    int i,j,k,sum,temp;
    for(i=0; i<length; i++)
    {
        for(j=0; j<attributenumber; j++)
        {
            sum = 0;
            for(k=strlen(txtfile[i].attribute[j]); k>0; k--)
            {
                temp = (int)(txtfile[i].attribute[j][k-1])-48;
                sum = sum*10+temp;
            }
            txtfile[i].attribute_int[j][0] = sum;
        }
    }
}

//从from[]中拷贝字符mark左边的字符串到to[]中(不含字符mark)
char *cutleft(char from[], char mark, char to[])
{
    int i;
    char *ptr;
    ptr = strchr(from,mark);
    for (i=0; i<ptr-from; i++)
        to[i] = from[i];
    to[i] = '\0';
    //  printf("%s",to);
    return to;
}

//从from[]中拷贝字符mark右边边的字符串到to[]中(不含字符mark)
char *cutright(char from[], char mark, char to[])
{
    int i;
    char *ptr;
    ptr = strchr(from,mark);

    for (i=0; from[ptr-from+i+1] != '\0'; i++)
        to[i] = from[ptr-from+i+1];
    to[i] = '\0';
    return to;
}

void BuildFileNameArray(struct FileName_type txtfile[], char filename[], int fileorder)
{
    char string[100000],temp[100000];
    int i;
    strcpy(string, filename);
    for(i=0; i<attributenumber-1; i++)
    {
        strcpy(txtfile[fileorder].attribute[i],cutleft(string,'_',temp));
        strcpy(string,cutright(string,'_',temp));
    }
    strcpy(txtfile[fileorder].attribute[i],cutleft(string,'.',temp));
}

int Query(char path[])
{
    long Handle;
    struct _finddata_t FileInfo;
    int i = 0;

    //查找所有符合条件的文件
    if((Handle=_findfirst(path,&FileInfo))==-1L)
        printf("没有找到匹配的项目\n");
    else
    {
        BuildFileNameArray(txtfile,FileInfo.name,i++);
        while( _findnext(Handle,&FileInfo)==0)//将查到的文件名写入(以后写入无需清空原索引表中的信息,故以a方式打开)
        {
            BuildFileNameArray(txtfile,FileInfo.name,i++);
        }
        _findclose(Handle);
    }
    return i;
}
func.c
/* 
*  文件名称:filename.h 
*  文件标识:见配置管理计划书 
*  摘     要:遍历某文件加下%i_%i_%i_%i_%i.txt格式的文件,存储到数组中
* 
*  当前版本:1.0
*  作     者:whl 
*  完成日期:2012-11-17
*/ 

#include "func.h"
#include <string.h>
#include <stdio.h>
#include <IO.H>

//将文件名转换为整形的以便建树
void convert(struct FileName_type txtfile[],int length)
{
    int i,j,k,sum,temp;
    for(i=0; i<length; i++)
    {
        for(j=0; j<attributenumber; j++)
        {
            sum = 0;
            for(k=strlen(txtfile[i].attribute[j]); k>0; k--)
            {
                temp = (int)(txtfile[i].attribute[j][k-1])-48;
                sum = sum*10+temp;
            }
            txtfile[i].attribute_int[j][0] = sum;
        }
    }
}

//从from[]中拷贝字符mark左边的字符串到to[]中(不含字符mark)
char *cutleft(char from[], char mark, char to[])
{
    int i;
    char *ptr;
    ptr = strchr(from,mark);
    for (i=0; i<ptr-from; i++)
        to[i] = from[i];
    to[i] = '\0';
    //  printf("%s",to);
    return to;
}

//从from[]中拷贝字符mark右边边的字符串到to[]中(不含字符mark)
char *cutright(char from[], char mark, char to[])
{
    int i;
    char *ptr;
    ptr = strchr(from,mark);

    for (i=0; from[ptr-from+i+1] != '\0'; i++)
        to[i] = from[ptr-from+i+1];
    to[i] = '\0';
    return to;
}

void BuildFileNameArray(struct FileName_type txtfile[], char filename[], int fileorder)
{
    char string[100000],temp[100000];
    int i;
    strcpy(string, filename);
    for(i=0; i<attributenumber-1; i++)
    {
        strcpy(txtfile[fileorder].attribute[i],cutleft(string,'_',temp));
        strcpy(string,cutright(string,'_',temp));
    }
    strcpy(txtfile[fileorder].attribute[i],cutleft(string,'.',temp));
}

int Query(char path[])
{
    long Handle;
    struct _finddata_t FileInfo;
    int i = 0;

    //查找所有符合条件的文件
    if((Handle=_findfirst(path,&FileInfo))==-1L)
        printf("没有找到匹配的项目\n");
    else
    {
        BuildFileNameArray(txtfile,FileInfo.name,i++);
        while( _findnext(Handle,&FileInfo)==0)//将查到的文件名写入(以后写入无需清空原索引表中的信息,故以a方式打开)
        {
            BuildFileNameArray(txtfile,FileInfo.name,i++);
        }
        _findclose(Handle);
    }
    return i;
}
main.c
/* 
*  文件名称:filename.h 
*  文件标识:见配置管理计划书 
*  摘     要:遍历某文件加下%i_%i_%i_%i_%i.txt格式的文件,存储到数组中
* 
*  当前版本:1.0
*  作     者:whl 
*  完成日期:2012-11-17
*/ 
#include "func.h"
#include <stdlib.h>
int main()
{
    int i,length,array[100000];
    length = Query("c:\\whl\\*.txt");
    convert(txtfile,length);
    for(i=0; i<length; i++)
    {
        array[i] = txtfile[i].attribute_int[0][0];
        printf("array[%d]=%d\n",i,array[i]);
    }
    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/whl2012/p/2774762.html