Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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::wofstream outFile(L"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, const 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 wstring& inFileName, const wstring& findFirst, const wstring& findLast )
- {
- /*
- std::string findFirst = "testing" ;
- std::string findLast = "finish" ;
- */
- wstring inputLine ;
- int lineNum = 0 ;
- wchar_t buffer[2048];
- size_t found = 0;
- wifstream inFile;
- inFile.open (inFileName.c_str()); // Open The file
- if (inFile.is_open())
- {
- while( getline( inFile, inputLine ))
- {
- ++lineNum ;
- // printf ("Line len = %d\n ", inputLine.length());
- if( (found = inputLine.find(findFirst)) != wstring::npos )
- {
- cout << "###Line " << lineNum << " At Position [ " << found << " ]\n" ;
- swprintf_s(buffer, 2048, L"[%-5.5d] %s\n", lineNum, inputLine.c_str());
- outFile << buffer ;
- bool foundLast = 0;
- while( getline( inFile, inputLine ))
- {
- ++lineNum ;
- swprintf_s(buffer, 2048, L"[%-5.5d] %s\n", lineNum, inputLine.c_str());
- if( (found = inputLine.find(findLast)) != wstring::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()
- {
- wifstream inFile ;
- int startLine = 0;
- int endLine = 0;
- int lineSize = 0;
- wchar_t 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)
- {
- wprintf (L"Filename1 is %s\n", it->c_str());
- wprintf (L"Filename2 is %s\n", files.begin()->c_str());
- outFile << "\n------------------------------\n";
- //outFile << *it << endl;
- wcout << *it << endl;
- outFile << "\n------------------------------\n";
- processFiles(*it, L"testing", L"finish");
- getchar();
- }
- }
- outFile.close();
- getchar();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement