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

Traversing directory and iterators in c++

本文关键字:迭代器 遍历 c++      更新时间:2023-10-16

我是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 filen");
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 = %dn ", inputLine.length());
if( (found = inputLine.find(findFirst)) != std::string::npos )
{
std::cout << "###Line " << lineNum << " At Position [ " << found << " ]n" ;
sprintf_s(buffer, 2048, "[%-5.5d] %sn", lineNum, inputLine.c_str());
outFile << buffer ;
bool foundLast = 0;
while( std::getline( inFile, inputLine ))
{
++lineNum ;
sprintf_s(buffer, 2048, "[%-5.5d] %sn", 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 %sn", it->c_str());
printf ("Filename2 is %sn", 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();
}

让您的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");

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

编辑:你可以在这里找到一个应该编译的例子。