遍历c ++中的目录和迭代器

时间:2012-09-16 21:32:28

标签: c++ iterator traversal

我是C ++的绝对新手,3天前才开始用它编程。 我正在努力做到这一点:

  • 遍历X.X文件的目录(通常是),对于每个文件,执行以下操作:

    • 在文件中搜索字符串(findFirst),然后搜索到另一个字符串(findLast) - 文件将为HTML格式。

在这个选择中,我想执行几个任务(尚未编写) - 但它们将是以下内容:

  • 其中一个字符串将是我要写入的文件名。 - 所以提取此字段并创建一个名为
  • 的输出文件
  • 部分行将是制造商部件号 - 提取这些行并相应地格式化输出文件 大部分都是产品描述。再次 - 这将在HTML构造中 - 因此解压缩并格式化输出文件。

到目前为止,我已经设法使用互联网上的一些帮助来运行遍历目录,并选择开始和结束关键字。

我的问题在这里 processFiles(inputFileName, "testing", "finish");

我需要inputFileName作为遍历文件名的名称。

我发现的所有示例都只是使用cout打印文件名 我需要将它传递给processFiles函数。

有人可以告诉我我需要使用什么吗?我尝试了it->c_Str()以及(*it)和.at.begin等的其他变体

我的非打印示例如下:

// Chomp.cpp : Defines the entry point for the console application.
//

#include <stdafx.h>
#include <windows.h>
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <cctype>
#include <algorithm>
#include <vector>
#include <stack>

//std::ifstream inFile ( "c:/temp/input.txt" ) ;
std::ofstream outFile( "c:/temp/output.txt") ; 

using namespace std;

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

void openFiles()
{
    if (!(outFile.is_open()))
    {
       printf ("Could not Create Output file\n");
       exit(0);
    }
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

bool ListFiles(wstring path, wstring mask, vector<wstring>& files) 
{
    HANDLE hFind = INVALID_HANDLE_VALUE;
    WIN32_FIND_DATA ffd;
    wstring spec;
    stack<wstring> directories;

    directories.push(path);
    files.clear();

    while (!directories.empty()) 
    {
        path = directories.top();
        spec = path + L"\\" + mask;
        directories.pop();

        hFind = FindFirstFile(spec.c_str(), &ffd);
        if (hFind == INVALID_HANDLE_VALUE)  
            return false;

        do 
        {
            if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0) 
            {
                if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
                    directories.push(path + L"\\" + ffd.cFileName);
                else 
                    files.push_back(path + L"\\" + ffd.cFileName);
            }
        } while (FindNextFile(hFind, &ffd) != 0);

        if (GetLastError() != ERROR_NO_MORE_FILES) 
        {
            FindClose(hFind);
            return false;
        }

        FindClose(hFind);
        hFind = INVALID_HANDLE_VALUE;
    }

    return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////

void processFiles(const wchar_t *inFileName, std::string findFirst,std::string findLast )
{
/*     
       std::string findFirst = "testing" ;
       std::string findLast  = "finish" ;
*/       
       std::string inputLine ;
       int lineNum = 0 ;
       char buffer[2048];
       size_t found = 0;

       std::ifstream inFile;
       inFile.open (inFileName);        // Open The file

       if (inFile.is_open())
       {
          while( std::getline( inFile, inputLine ))
          {
             ++lineNum ;
 //          printf ("Line len = %d\n ", inputLine.length());

             if( (found = inputLine.find(findFirst)) != std::string::npos )
             {
                 std::cout << "###Line " << lineNum << " At Position [ " << found << " ]\n" ;
                 sprintf_s(buffer, 2048, "[%-5.5d] %s\n", lineNum, inputLine.c_str());
                 outFile << buffer ;

                 bool foundLast = 0;
                 while( std::getline( inFile, inputLine ))
                 {
                     ++lineNum ;

                     sprintf_s(buffer, 2048, "[%-5.5d] %s\n", lineNum, inputLine.c_str());
                     if( (found = inputLine.find(findLast)) != std::string::npos )
                     {
                         outFile << buffer ;
                         break;  // Found last string - so stop after printing last line
                     }
                     else
                          outFile << buffer ;
                 }
             }
             else
             {
                // std::cout << "=>" << inputLine << '\n' ;
             }
          }
       }
       else
       {
             printf ("Cant open file \n");
             exit(0);
       }

       inFile.close() ;     // Close The file
}

/////////////////////////////////////////////////////////////////////////////////////////////
///                                   M    A    I    N
/////////////////////////////////////////////////////////////////////////////////////////////

int main()
{
       std::ifstream inFile ;

       int startLine = 0;
       int endLine = 0;
       int lineSize = 0;
       char buffer[512];

       vector<wstring> files;           // For Parsing Directory structure

       openFiles();

       // Start The Recursive parsing of Directory Structure 

       if (ListFiles(L"C:\\temp", L"*.*", files)) 
       {
            for (vector<wstring>::iterator it = files.begin(); it != files.end(); ++it) 
            {

                printf ("Filename1 is %s\n", it->c_str());
                printf ("Filename2 is %s\n", files.begin());

                outFile <<  "\n------------------------------\n";
                //outFile << it  << endl;
                wcout << it->c_str() << endl;
                outFile <<  "\n------------------------------\n";


                const wchar_t *inputFileName = it->c_str();
//              processFiles(inputFileName, "testing", "finish");
//              getchar();
            }
       }
       outFile.close();
       getchar();
}

2 个答案:

答案 0 :(得分:0)

让你的processFile接受一个wstring,即:

void processFiles(wstring inFileName, std::string findFirst,std::string findLast )
{
  // Make the necessary changes so that you use a wstring for inFileName
}

使用:

从main()调用它
processFiles(*it, "testing", "finish");

答案 1 :(得分:0)

您需要更改processFile以使用wifstream而不是ifstream,您应该更改所有窄字符串以使用宽字符串(反之亦然)。窄字符串和宽字符串彼此不兼容,为了使用另一个字符串,必须使用转换函数,例如mbstowcs。

编辑: 您可以找到应编译here的示例。