打印路径下的所有文件路径

直接上代码(我用的是Dev-C++ 编辑器写的,新手请勿喷)

#include <io.h>  
#include <iostream>  
#include <vector>  

#include <fstream>//文件与流 
#include <sstream>//转类型char string 
#include <ctime>//时间 

//#include <cstring>
#include <string>
using namespace std;


void getFiles(string path, vector<string>& files)
{
    long   hFile = 0; //文件句柄
    struct _finddata_t fileinfo;//文件信息,声明一个存储文件信息的结构体  
    string p;//字符串,存放路径
    if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
    {
        do
        {//如果是目录,迭代之(即文件夹内还有文件夹)  
            if ((fileinfo.attrib &  _A_SUBDIR))
            {//文件名不等于"."&&文件名不等于".."
                //.表示当前目录
                //..表示当前目录的父目录
                //判断时,两者都要忽略,不然就无限递归跳不出去了!
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                    getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
            } else//如果不是,加入列表 
            {
                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile); //_findclose函数结束查找
    }
}

//时间拼接:年_月_日_时_分_秒  作为:创建文件名 
string pinjie(time_t *now){
    //函数不要参数写法/////////////////////////////////////
    //    time_t now = time(0);//   now   1970 到目前经过秒数: 
    //     tm *ltm = localtime(&now);
    ///////////////////////////////////////////////// 
     tm *ltm = localtime(now);
   string pj ="";
    //int-->string:  年     月      日      时      分    秒 
     stringstream stream1,stream2,stream3,stream4,stream5,stream6;  
     stream1<<(1900 + ltm->tm_year);//年
     pj+=stream1.str();
     stream2<<(1 + ltm->tm_mon);//月
     pj+=stream2.str();
     stream3<<(ltm->tm_mday);//日 
     pj+=stream3.str();
     stream4<<(ltm->tm_hour);//时 
     pj+=stream4.str();
     stream5<<(ltm->tm_min);//分 
     pj+=stream5.str();
     stream6<<(ltm->tm_sec);//秒 
     pj+=stream6.str();
    return pj;    

//案例一  输出对应路径文件名 
void anlione(){
    
//    time_t time(time_t *time);
//    //该函数返回一个指向 time 的指针,time 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
//    struct tm *gmtime(const time_t *time);
//     //该函数返回一个指向字符串的指针,字符串包含了 time 所指向结构中存储的信息,返回形式为:day month date hours:minutes:seconds year\n\0。
//    char * asctime ( const struct tm * time );
//    //该返回一个表示当地时间的字符串指针,字符串形式 day month year hours:minutes:seconds year\n\0。
//    char *ctime(const time_t *time);
//    
   //基于当前系统的当前日期/时间
   time_t now = time(0);//   now   1970 到目前经过秒数:
   time_t *nowzz=NULL;//定义 time_t型指针 
   nowzz=&now;//值的赋值 
   
   // 把 now 转换为字符串形式
   char* dtbd = ctime(&now);
   cout << "本地日期和时间:" << dtbd << "\r";//北京时间    \n\r  换行 
   // 把 now 转换为 tm 结构
   tm *gmtm = gmtime(&now);
   char* dtUTC = asctime(gmtm);
   
   cout << "UTC 日期和时间:"<< dtUTC << endl;//相当于国际时间 
   
   //urlstr 就是是文件输出的绝对路径  我这时把查出的文件路径输出到txt里。(这个你可以改为自己的) 
   string urlstr="C:\\\\Users\\\\曾国喜\\\\Desktop\\\\"+pinjie(nowzz)+".txt";//C:\\Users\\曾国喜\\Desktop\\xxxxx.txt
   const char *urlchar=urlstr.c_str();
     
  cout<<"输入对应路径 ( 如:E:\\图片总  、尽量不要写根级别的路径,否则输出炸哈哈!!!!):"<<endl;
  string filePath="";//char * filePath=""; //有些地方默认他们一样  ;//自己设置目录  如"E:\\图片总"或"E:\图片总" 
  getline(cin,filePath);
  void close();//关闭流 
  
     string text_tou="获取路径:"+filePath+"\n\r"+"本地时间:"+dtbd+"\r";
     const char *text_touchar=text_tou.c_str();
    //写入文本 如果没就默认创建文本 
    ofstream outfile;
    outfile.open(urlchar, ios::out | ios::app);//以追加模式写入对应位置 没文件,则创建文件  //第一个参数 应是char xxx=""; 
    outfile << text_touchar << endl;//写入输出的text头部信息 
    vector<string> files;
    ////获取该路径下的所有文件  
    getFiles(filePath, files);
    char str[30];
    int size = files.size();
    for (int i = 0; i < size; i++)
    {
        cout << files[i].c_str() << endl;//打印文件 名 
        outfile << files[i].c_str() << endl;// 向文件写入 文件名的数据
    }
    //system("cls");//清空显示屏 内容     


int main(){    
    anlione();//案例一  输出对应路径文件名 
  
}
 

猜你喜欢

转载自blog.csdn.net/weixin_44540005/article/details/86549159
今日推荐