c++ 遍历目录 去掉隐藏文件

根据string特性:

1. size_t find (const string& str, size_t pos = 0)

str.find(str1)

说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos
参考:find函数:http://www.cplusplus.com/reference/string/string/find/

2. size_t find_first_of (const string& str, size_t pos = 0)

str.find_first_of(str1)

说明:从pos(默认是是0,即从头开始查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的起始索引位置;如果没有找到则返回string::npos
参考:find_first_of函数:http://www.cplusplus.com/reference/string/string/find_first_of/

3.size_t find_last_of (const string& str, size_t pos = npos)

str.find_last_of(str1)

说明:从npos(默认是字符串最后一个,即从后向前查找)开始查找,找到第一个和str1相匹配的子串,返回该子串的最后一个字符的索引位置;如果没有找到则返回string::npos
参考: find_last_of函数http://www.cplusplus.com/reference/string/string/find_last_of/

遍历文件去掉"." ".." 隐藏文件 :程序

#include <stdio.h>  
#include <sys/types.h>  
#include <dirent.h>  
#include <iostream>
#include <string.h>
  
int main()  
{  
    DIR* pDir;  
    struct dirent* ptr;  

    if( !(pDir = opendir("./")) )  
        return -1;  
    while( (ptr = readdir(pDir)) != 0 )  
    {  
      std::string s = ptr->d_name;

      if(((std::string)ptr->d_name).find_first_of("."))
        continue;
       /*Processing File*/ 

      std::cout << "file name=" << ptr->d_name
                 << ", ino=" << ptr->d_ino
                 << ", reclen=" << ptr->d_reclen
                 << std::endl; 

    }  
    closedir(pDir);  
    return 0;  
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值