zoukankan      html  css  js  c++  java
  • 遍历文件夹

    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;  
    }  
  • 相关阅读:
    每日一题_191118
    每日一题_191117
    每日一题_191116
    每日一题_191115
    每日一题_191114
    每日一题_191113
    每日一题_191112
    每日一题_191111
    每日一题_191110
    一道抛物线自编题的思考
  • 原文地址:https://www.cnblogs.com/raorao1994/p/8874428.html
Copyright © 2011-2022 走看看