C++边角料

这篇博客介绍了如何使用C++来判断文件是否存在,以及如何遍历指定文件下的所有子文件,包括非递归和递归两种方式。在不递归的情况下,会获取到包括'.'和'..'在内的子文件名;递归遍历则能获取所有子文件路径名。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:C++判断文件是否存在[1][2]

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{

    string filePath = "C:\\faceTest.txt";//文件路径
    fstream fileStream;                 //定义文件流
    fileStream.open(filePath, ios::in);//文件以输入方式打开(文件数据输入到内存)
    if(!fileStream)
    {
        cout<<filePath<<" does not exist!"<<endl;
    }
    else
    {
        cout<<filePath<<" exists!"<<endl;
    }
    return 0;
}

2:遍历指定文件下子文件,得到文件名[3]

(1)不递归,仅记录当前文件目录子文件名

#include <iostream>
#include <string>
#include <vector>
#include <io.h>                                                 //必须的头文件
using namespace std;
void GetSubFilesPath( string path, vector<string>& files);  
int main()
{

    string Bigfilepath("C:\\face");
    vector<string> path;
    GetSubFilesPath(Bigfilepath, path);
    return 0;
}
void GetSubFilesPath(string path, vector<string>& files)  
{  
    //文件句柄  
    long long hFile = 0; //如果句柄是long 或者int 只能用于32位,而long long 在32位和64位都可以  
    //文件信息  
    struct _finddata_t fileinfo;  
    string p;//临时定义string 字符串,保存子文件完整路径  
    hFile = _findfirst(p.assign(path).append("\\*").c_str(),&fileinfo);
    if(hFile != -1)
    {
        do
        {
            files.push_back(p.assign(path).append("\\").append(fileinfo.name));
        }while(_findnext(hFile, &fileinfo) == 0);
    }
    _findclose(hFile);  
}

注意:files保存着所有文件名,但前两项是类似系统文件(.和..),后面才是子文件路径名。

(2)递归直到非目录文件,得到所有子文件路径名

#include <iostream>
#include <string>
#include <vector>
#include <io.h>                                                 //必须的头文件
using namespace std;
void GetAllSubFilesPath( string path, vector<string>& files);  
int main()
{

    string Bigfilepath("C:\\faceTest");
    vector<string> path;
    GetAllSubFilesPath(Bigfilepath, path);
    return 0;
}
void GetAllSubFilesPath(string path, vector<string>& files)  
{  
    //文件句柄  
    long hFile = 0;  
    //文件信息  
    struct _finddata_t fileinfo;  
    string p;//临时定义string 字符串,保存子文件完整路径  
    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)  
                     GetAllSubFilesPath( 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);  
    }  
}

注意:path下全部是子文件名。

[1]http://blog.youkuaiyun.com/roger_77/article/details/1538447/
[2]http://www.cnblogs.com/shaoguobao/archive/2011/04/15/2017413.html
[3]http://blog.youkuaiyun.com/xuejiren/article/details/37040827

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值