在c+中遍历目录和迭代器+; 我是C++的绝对新手,3天前才开始用它编程。 我正试着做这个蠢事:

在c+中遍历目录和迭代器+; 我是C++的绝对新手,3天前才开始用它编程。 我正试着做这个蠢事:,c++,iterator,traversal,C++,Iterator,Traversal,遍历X.X文件(通常)的目录,并对每个文件执行以下操作: 在文件中搜索一个字符串(findFirst),然后搜索到另一个字符串(findLast)-文件将为HTML格式 在此选择中,我希望执行几个任务(尚未编写),但它们将是: 其中一个字符串将是我要写入的文件名。-因此,提取此字段并创建一个具有此名称的输出文件 其中一些行将是制造商零件号-提取这些零件号并相应地格式化输出文件 大部分内容都是对产品的描述。同样-这将是一个HTML构造-所以提取这个并格式化输出文件 到目前为止,我已经设法

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

  • 在文件中搜索一个字符串(
    findFirst
    ),然后搜索到另一个字符串(
    findLast
    )-文件将为HTML格式
在此选择中,我希望执行几个任务(尚未编写),但它们将是:

  • 其中一个字符串将是我要写入的文件名。-因此,提取此字段并创建一个具有此名称的输出文件
  • 其中一些行将是制造商零件号-提取这些零件号并相应地格式化输出文件 大部分内容都是对产品的描述。同样-这将是一个HTML构造-所以提取这个并格式化输出文件
到目前为止,我已经设法使用了traverse目录,并选择了start和finish关键字——使用了来自internet的一些帮助

我的问题在这里
processFiles(输入文件名,“测试”,“完成”)

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

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

有人能告诉我需要用什么吗?我试过
it->c_Str()
和(
*it
)和
的其他变体

我的非打印示例如下:

// 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();
}
//Chomp.cpp:定义控制台应用程序的入口点。
//
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
//std::ifstream infle(“c:/temp/input.txt”);
标准::流出流文件(“c:/temp/output.txt”);
使用名称空间std;
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
void openFiles()
{
如果(!(outFile.is_open())
{
printf(“无法创建输出文件\n”);
出口(0);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
bool列表文件(wstring路径、wstring掩码、向量和文件)
{
句柄hFind=无效的句柄值;
WIN32_查找_数据ffd;
wstring规范;
堆栈目录;
目录。推送(路径);
clear()文件;
而(!directories.empty())
{
path=directories.top();
spec=路径+L“\\”+掩码;
目录pop();
hFind=FindFirstFile(规范c_str(),&ffd);
if(hFind==无效的句柄值)
返回false;
做
{
如果(wcscmp(ffd.cFileName,L.“)!=0和&wcscmp(ffd.cFileName,L.“)!=0)
{
if(ffd.dwFileAttributes和文件属性目录)
目录.push(路径+L“\\”+ffd.cFileName);
其他的
文件。向后推(路径+L“\\”+ffd.cFileName);
}
}while(FindNextFile(hFind,&ffd)!=0);
如果(GetLastError()!=错误\u没有\u更多\u文件)
{
FindClose(hFind);
返回false;
}
FindClose(hFind);
hFind=无效的句柄值;
}
返回true;
}
/////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////
无效进程文件(常量wchar\u t*inFileName,std::string findFirst,std::string findLast)
{
/*     
std::string findFirst=“测试”;
std::string findLast=“finish”;
*/       
std::字符串输入线;
int lineNum=0;
字符缓冲区[2048];
找到的大小=0;
std::ifstream-infle;
inFile.open(inFileName);//打开文件
if(infle.is_open())
{
while(std::getline(填充、输入行))
{
++莱纳姆;
//printf(“行len=%d\n”,inputLine.length());
if((find=inputLine.find(findFirst))!=std::string::npos)
{

std::cout使您的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()调用它:


您需要将processFile更改为使用wifstream而不是ifstream,并且您应该将所有窄字符串更改为使用宽字符串(反之亦然)。窄字符串和宽字符串彼此不兼容,为了将两者结合使用,必须使用转换函数,如mbstowcs

编辑:
您可以找到一个应该编译的示例。

C++有一套非常好的库,非常便于移植:[Boost](www.Boost.org),特别是[Boost Filesystem](www.Boost.org/libs/Filesystem/)库包含了很多你编写的功能。例如,它有一个<代码> RealsivyDigryyTythyAuthor 类,它只需要一个起始位置。嗨,感谢使用Boost的建议。我已经看到Boost了,但不确定是否对C++保持真实性,只使用标准函数,也不确定是否也适用于任何ANSWE。rs建议,如果有少量代码(如我所说,我是一个绝对的新手),我们将不胜感激。谢谢你的输入,但当我这样做时,我的file.open失败,并显示消息“Error 1 Error C2664:”void std::basic_ifstream::open(const wchar_t*,std::ios_base::openmode,int)“:无法将参数1从'std::wstring'转换为'const wchar_t*'我很快尝试将ifstream转换为wifstream,但随后出现了许多对我来说毫无意义的错误。错误C2664:'void std::basic_ifstream::open(const wchar_t*,std::ios_base::openmode,int)':无法从'st'转换参数1”
processFiles(*it, "testing", "finish");