遍历文件夹

void getFiles(string path, vector<string>& files)
{
    intptr_t   hFile = 0;
    struct _finddata_t fileinfo;
    string p;
    long i;
    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); } }

 读取文件

#include <fstream>  
#include <string>  
   
using namespace std;  
   
int main()  
{  
    vector<string> vecContent;  
    string strLine;  
    ifstream inFile("e:\test.txt");  
    while (inFile)  
    {  
        getline(inFile, strLine);  
        vecContent.push_back(strLine);  
    }  
    inFile.close();  
   
    // 删除第一行  
    vecContent.erase(vecContent.begin());  
   
    ofstream outFile("e:\test.txt");  
    vector<string>::const_iterator iter = vecContent.begin();  
    for (; vecContent.end() != iter; ++iter)  
    {  
        outFile.write((*iter).c_str(), (*iter).size());  
        outFile << '
';  
    }  
   
    outFile.close();  
   
    return 0;  
}  
原文地址:https://www.cnblogs.com/raorao1994/p/8874428.html