How can i get current file name without path, i want to use it on WinExec,
example for what am trying to do,
WinExec("Do something to <mycurrentfilename.exe>", SW_HIDE);
In common case you have to use GetModuleFileName function.
Example:
#include <Windows.h>
#include <sstream>
int main(void) {
char myexepath[MAX_PATH] = { 0 };
DWORD returnCode = GetModuleFileNameA(NULL, myexepath, MAX_PATH);
if (returnCode != 0 && returnCode < MAX_PATH)
{
std::string filepath(myexepath);
filepath = filepath.substr(filepath.find_last_of('\\') + 1);
std::ostringstream ss;
ss << "Do something to \"" << filepath << "\"";
WinExec(ss.str().c_str(), SW_HIDE);
}
else
{
// process GetModuleFileName error.
}
return 0;
}
Example uses char encoding for filename, but it can be changed to wchar_t or universal TCHAR
Related
So I am trying to create a function to download a file with a variable address depending on the user with a string input using URLDownloadToFile() however it does not seem to actually download the file to the disk. It skips S_OK, E_OUTOFMEMORY and INET_E_DOWNLOAD_FAILURE to UNKNOWN_ERROR. Using GetLastError() returns 2, checking up online said that meant "Not Found". I'm not sure whether this was the website I was using or not (it's my own server with Node.JS using res.download(path, filename)). Hope for help, the code is below.
Just a note, everything is properly included as needed and the process (safely) runs with SE_DEBUG_NAME so it hopefully should not be a permission error.
// Convert String To Wide String
wstring S2WS(const string& str) {
int length;
int slength = (int)str.length() + 1;
length = MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, 0, 0);
wchar_t* buffer = new wchar_t[length];
MultiByteToWideChar(CP_ACP, 0, str.c_str(), slength, buffer, length);
wstring out(buffer);
delete[] buffer;
return out;
}
// Download File
int download(string url) {
wstring wUrl = S2WS(url);
LPCWSTR lUrl = wUrl.c_str();
TCHAR path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, path);
HRESULT response = URLDownloadToFile(NULL, lUrl, path, 0, NULL);
if (response == S_OK) {
cout << "S_OK" << endl;
} else if (response == E_OUTOFMEMORY) {
cout << "E_OUTOFMEMORY" << endl;
} else if (response == INET_E_DOWNLOAD_FAILURE) {
cout << "INET_E_DOWNLOAD_FAILURE" << endl;
} else {
cout << "UNKNOWN_ERROR" << endl;
}
return 0;
}
// Example Usage
int main() {
download("http://somewebsite.com/files/textfile.txt");
}
From URLDownloadToFile, the path should be file path not a directory path. You could append some file name to the path variable and it should work.
TCHAR path[MAX_PATH];
GetCurrentDirectory(MAX_PATH, path);
wcscat_s(path, L"\\filename.txt");
I'm new in c++. I'm trying to list files in dir. I'm using unicode. The problem is not listing files but treat string and paths with wchar*, I'm going mad. Here's my test code:
#define UNICODE 1
#include <stdio.h>
#include <windows.h>
#include <wchar.h>
int wmain(int argc,wchar_t **argv){
if (argc > 1){
wchar_t* path=argv[1];
wchar_t* pwc;
int last_occurence;
pwc=wcsrchr(path,L'\\');
last_occurence = pwc-path+1;
int len = wcslen(path);
if (last_occurence == len){
//slash
}else{
//no slash
wcscat(path,L"\\");
}
wcscat(path,L"*");
WIN32_FIND_DATA FindData;
HANDLE hSearch;
hSearch = FindFirstFile(path , &FindData);
if(hSearch == INVALID_HANDLE_VALUE){
return -1;
}else{
// *** PROBLEM STARTS HERE
wchar_t* filePath=NULL;
do{
wcscpy(filePath,path);
wcscat(filePath,FindData.cFileName);
wprintf(L"Path %s\n",filePath);
memset(filePath, '\0', wcslen(filePath));
// *** PROBLEM ENDS HERE
}while( FindNextFile(hSearch, &FindData) > 0 );
int ret = FindClose(hSearch);
return 0;
}
}
}
When I run the compiled app, it stops responding. What I would like to do is print the path I pass to my app (c:\dir1\dir2) and append the files in it (file1,file2) as follows:
c:\dir1\dir2\file1
c:\dir1\dir2\file2
How to solve this problem? There are best methods to do something like this? I would remain with wchar not std string if possible
There are a few issues with your code.
you are concatenating "\\*" to the memory that argv[1] points to, which is bad. You need to change path from a wchar_t* pointer to an wchar_t[] array, and then wcscpy the argv[1] data into it.
you are not allocating any memory for filePath, so wcscpy() and wcscat() are writing to invalid memory. You need to change filePath to a wchar_t[] array as well, and then wcscpy/wcscat the path data into it.
you are also not ignoring the concatenated * when combining the path and cFileNames values together.
you don't need the memset() at all (especially since you are giving it the wrong byte count anyway).
Try something more like this instead:
#define UNICODE 1
#include <stdio.h>
#include <windows.h>
#include <wchar.h>
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
wprintf(L"Usage: \"%s\" path\n", argv[0]);
return -1;
}
int len = wcslen(argv[1]);
if (len >= MAX_PATH)
{
wprintf(L"Path is too long\n");
return -1;
}
wchar_t path[MAX_PATH+1] = {};
wcscpy(path, argv[1]);
if ((len > 0) && (path[len-1] != L'\\'))
wcscat(path, L"\\");
wchar_t searchMask[MAX_PATH+2] = {};
wcscpy(searchMask, path);
wcscat(searchMask, L"*");
WIN32_FIND_DATA FindData;
HANDLE hSearch = FindFirstFileW(searchMask, &FindData);
if (hSearch == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
wprintf(L"Error looking for first file\n");
return -1;
}
wprintf(L"No files found\n");
}
else
{
wchar_t filePath[MAX_PATH*2];
do
{
wcscpy(filePath, path);
wcscat(filePath, FindData.cFileName);
wprintf(L"Path %s\n", filePath);
}
while (FindNextFileW(hSearch, &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
FindClose(hSearch);
wprintf(L"Error looking for next file\n");
return -1;
}
FindClose(hSearch);
}
return 0;
}
Though, you really should be using the std::unique_ptr and std::wstring classes and let them manage memory/resources for you. Using C library functions is not helping you learn C++:
#define UNICODE 1
#include <windows.h>
#include <wchar.h>
#include <iostream>
#include <string>
#include <memory>
struct FindDeleter
{
typedef HANDLE pointer;
void operator()(HANDLE h)
{
if(h != INVALID_HANDLE_VALUE)
FindClose(h);
}
};
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
std::wcerr << L"Usage: \"" << argv[0] << L"\" path" << std::endl;
return -1;
}
std::wstring path = argv[1];
if ((!path.empty()) && (path[path.length()-1] != L'\\'))
path += L'\\';
WIN32_FIND_DATA FindData;
std::unique_ptr<HANDLE, FindDeleter> hSearch(FindFirstFileW((path + L"*").c_str(), &FindData));
if (hSearch.get() == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
std::wcerr << L"Error looking for first file" << std::endl;
return -1;
}
std::wcout << L"No files found" << std::endl;
}
else
{
do
{
std::wstring filePath = path + FindData.cFileName;
std::wcout << L"Path " << filePath << std::endl;
}
while (FindNextFileW(hSearch.get(), &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
std::wcerr << L"Error looking for next file" << std::endl;
return -1;
}
}
return 0;
}
Or, if you are not using a C++11 compiler:
#define UNICODE 1
#include <windows.h>
#include <wchar.h>
#include <iostream>
#include <string>
class FindHandle
{
private:
HANDLE m_hFind;
public:
FindHandle(HANDLE hFind) : m_hFind(hFind) {}
~FindHandle()
{
if (m_hFind != INVALID_HANDLE_VALUE)
FindClose(m_hFind);
}
HANDLE get() { return m_hFind; }
};
int wmain(int argc, wchar_t **argv)
{
if (argc < 2)
{
std::wcerr << L"Usage: \"" << argv[0] << L"\" path" << std::endl;
return -1;
}
std::wstring path = argv[1];
if ((!path.empty()) && (path[path.length()-1] != L'\\'))
path += L'\\';
WIN32_FIND_DATA FindData;
FindHandle hSearch(FindFirstFileW((path + L"*").c_str(), &FindData));
if (hSearch.get() == INVALID_HANDLE_VALUE)
{
if (GetLastError() != ERROR_FILE_NOT_FOUND)
{
std::wcerr << L"Error looking for first file" << std::endl;
return -1;
}
std::wcout << L"No files found" << std::endl;
}
else
{
do
{
std::wstring filePath = path + FindData.cFileName;
std::wcout << L"Path " << filePath << std::endl;
}
while (FindNextFileW(hSearch.get(), &FindData));
if (GetLastError() != ERROR_NO_MORE_FILES)
{
std::wcerr << L"Error looking for next file" << std::endl;
return -1;
}
}
return 0;
}
I am trying to search the registry's uninstall folder for a given program that I know is there by checking regedit. My function currently finds the program, but for some reason it doesn't update the output value from the RegGetValue function until the next iteration. So it prints the correct registry key and its predecessor. Any ideas?
I am on a Windows 10 64bit workstation with Intel processors using Visual Studio 2015 if that matters.
main.cpp
#include "Registry.h"
#include <Windows.h>
#include <tchar.h>
void main()
{
Registry test(_T("Microsoft SQL Server 2012 Native Client "));
}
Registry.h
#pragma once
#include <Windows.h>
#include <string>
#define REG_PATH L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
#define X86REG_PATH L"SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
class Registry
{
public:
Registry(TCHAR* name);
~Registry();
TCHAR* findGUID();
TCHAR* getDisplayName();
TCHAR* getGUID();
TCHAR* getVersion();
TCHAR* getPublisher();
TCHAR* getInstallDate();
private:
TCHAR* displayName;
TCHAR* guid;
TCHAR* version;
TCHAR* publisher;
TCHAR* installDate;
};
Registry.cpp
#pragma once
#include "Registry.h"
#include <Windows.h>
#include <iostream>
#include <tchar.h>
#include <fstream>
Registry::Registry(TCHAR* name)
{
HKEY hKey;
LPCTSTR lpSubKey;
DWORD ulOptions;
REGSAM samDesired;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
{
std::wofstream file;
file.open("test.txt");
int index = 0;
HKEY UninstallDir = hKey;
TCHAR subKey[MAX_PATH];
DWORD subKeySize = MAX_PATH;
while (RegEnumKeyEx(hKey, index, subKey, &subKeySize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
HKEY guid;
TCHAR* guidPath = new TCHAR[MAX_PATH];
_tcscpy_s(guidPath, MAX_PATH, REG_PATH);
_tcscat_s(guidPath, MAX_PATH, subKey);
TCHAR compareName[MAX_PATH];
DWORD nameSize;
//print all registry keys to file
file << index << ": " << guidPath << std::endl;
int test;
RegGetValue(HKEY_LOCAL_MACHINE, guidPath, _T("DisplayName"), RRF_RT_ANY, NULL, &compareName, &nameSize);
//compare all registry keys *temporary to debug
if (_tcscmp(guidPath, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{49D665A2-4C2A-476E-9AB8-FCC425F526FC}")) == 0)
{
std::wcout << guidPath << " found" << std::endl;
}
if (_tcscmp(compareName, name) == 0)
{
_tprintf(_T("%d: %s\n"), index, guidPath);
}
//print if found
index++;
subKeySize = 260;
}
file.close();
}
else
{
std::cout << "Could not open registry key." << std::endl;
}
//temporary to see console
std::cin.get();
}
//still need to be completed
Registry::~Registry()
{
}
TCHAR* Registry::findGUID()
{
return _T("");
}
TCHAR* Registry::getDisplayName()
{
return _T("");
}
TCHAR* Registry::getGUID()
{
return _T("");
}
TCHAR* Registry::getVersion()
{
return _T("");
}
TCHAR* Registry::getPublisher()
{
return _T("");
}
TCHAR* Registry::getInstallDate()
{
return _T("");
}
I see a lot of problems with your code.
You are mixing std::wcout and _tprintf(), which causes buffering conflicts.
You are mixing char and wchar_t data incorrectly.
You are leaking guidPath on every loop iteration.
You are not initializing nameSize when calling RegGetValue().
You are not setting up your code to access the 32bit Wow64Node key correctly.
Try something more like this instead.
main.cpp
#include <Windows.h>
#include "Registry.h"
void main()
{
Registry test(L"Microsoft SQL Server 2012 Native Client");
}
Registry.h
#pragma once
#include <Windows.h>
#include <string>
class Registry
{
public:
Registry(const std::wstring &name);
~Registry();
std::wstring findGUID();
std::wstring getDisplayName();
std::wstring getGUID();
std::wstring getVersion();
std::wstring getPublisher();
std::wstring getInstallDate();
private:
std::wstring displayName;
std::wstring guid;
std::wstring version;
std::wstring publisher;
std::wstring installDate;
};
Registry.cpp
#pragma once
#include <Windows.h>
#include "Registry.h"
#include <iostream>
#include <fstream>
#define REG_PATH L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\"
Registry::Registry(const std::wstring &name)
{
HKEY hKey;
// If you want to open the 32bit Wow64Node key,
// DO NOT open the key directly! Open the 64bit
// key and include the KEY_WOW64_32KEY flag
// so it will redirect to the Wow64Node key...
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, REG_PATH, NULL, KEY_READ, &hKey) == ERROR_SUCCESS)
{
std::wofstream file;
file.open(L"test.txt");
WCHAR subKey[MAX_PATH];
DWORD subKeySize = MAX_PATH;
WCHAR compareName[MAX_PATH];
DWORD nameSize;
int index = 0;
while (RegEnumKeyEx(hKey, index, subKey, &subKeySize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
// print all registry keys to file
file << index << L": " << REG_PATH << subKey << std::endl;
int test;
nameSize = sizeof(compareName);
RegGetValue(hKey, NULL, L"DisplayName", RRF_RT_REG_SZ | RRF_RT_REG_EXPAND_SZ | RRF_ZEROONFAILURE, NULL, compareName, &nameSize);
//compare all registry keys *temporary to debug
if (wcscmp(subKey, L"{49D665A2-4C2A-476E-9AB8-FCC425F526FC}") == 0)
{
std::wcout << subKey << L" found" << std::endl;
}
if (name == compareName)
{
std::wcout << name << L" found" << std::endl;
}
//print if found
index++;
subKeySize = MAX_PATH;
}
file.close();
}
else
{
std::wcout << L"Could not open registry key." << std::endl;
}
//temporary to see console
std::wcin.get();
}
//still need to be completed
Registry::~Registry()
{
}
std::wstring Registry::findGUID()
{
return L"";
}
std::wstring Registry::getDisplayName()
{
return L"";
}
std::wstring Registry::getGUID()
{
return L"";
}
std::wstring Registry::getVersion()
{
return L"";
}
std::wstring Registry::getPublisher()
{
return L"";
}
std::wstring Registry::getInstallDate()
{
return L"";
}
First off, I googled the hell out of this then searched the forums. In my ignorance of how the TEXT() function operates I cannot find an efficient way to search for an answer.
I writing a piece of code to search for a file that relies on inputting the directory you want to search. However, when I pass anything but a literal value to the function, like displayContent(_TEXT("c:\")), the software does not execute properly. It does not search for anything. Inserting breakpoints doesn't tell much, as the software closes anyways.
I would like to pass a variable to the the displayContent function by placing TEXT(variable) inside its argument like displayContent(_TEXT(*ptrDir)) but that is not compiling. Furthermore, when I simply place ptrDir inside of the argument of displayContent the software compiles but does not execute properly, as it asks for the directory to search but does not actually search it.
What's happening here? There has to be a way to pass a variable to displayContent that includes a string that's recieved from the user.
#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <tchar.h>
#include "Strsafe.h"
#include <string>
using namespace std;
typedef wchar_t WCHAR;
#define CONST const
typedef CONST WCHAR* LPCWSTR;
int displayContent(LPCWSTR lpszPath, int level = 0) {
wcout << lpszPath << endl;
getchar();
getchar();
WIN32_FIND_DATA ptrFileData;
HANDLE hFile = NULL;
BOOL bGetNext = TRUE;
wchar_t lpszNewPath[MAX_PATH];
if (lstrlen(lpszPath) > MAX_PATH)
return -1;
StringCchCopy(lpszNewPath, MAX_PATH, lpszPath);
StringCchCat(lpszNewPath, MAX_PATH, _TEXT("*.*"));
hFile = FindFirstFile(lpszNewPath, &ptrFileData);
while (bGetNext)
{
for (int i = 0; i < level; i++)
wcout << "-";
if (ptrFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY
&& lstrlen(ptrFileData.cFileName) > 2)
{
wchar_t lpszFirstTimePath[MAX_PATH];
StringCchCopy(lpszFirstTimePath, MAX_PATH, lpszPath);
StringCchCat(lpszFirstTimePath, MAX_PATH, ptrFileData.cFileName);
StringCchCat(lpszFirstTimePath, MAX_PATH, _TEXT("\\"));
wcout << ">" << ptrFileData.cFileName << endl;
displayContent(lpszFirstTimePath, level + 2);
}
else
{
wcout << ">" << ptrFileData.cFileName << endl;
}
bGetNext = FindNextFile(hFile, &ptrFileData);
}
FindClose(hFile);
return 0;
}
int main(int argc, char* argv[])
{
WCHAR directory;
LPCWSTR ptrDir;
ptrDir = &directory;
cout << "Enter directory you wish to search: " << endl;
//cin >> directory;
directory = 'c:\\' ;
ptrDir = &directory;
displayContent(_TEXT(*ptrDir));
getchar();
getchar();
return 0;
}
The _TEXT (and equivalently, _T) macro is strictly for literals (string literals or character literals). It expands to L for a Unicode build, and to nothing for a narrow-character build. So, for a string like (say) "hello", you'll get L"hello" for a Unicode build and "hello" for a narrow-character build. This gives you a wide literal in a Unicode build and a narrow literal otherwise.
If you have a string in a variable, you can convert between wide and narrow characters with the MultiByteToWideChar and WideCharToMultibyte functions.
In this case, doing a conversion on the contents of a variable isn't really needed though. After eliminating some unnecessary complexity, and using a few standard library types where they make sense, I end up with code something like this:
#include <iostream>
#include <tchar.h>
#include <string>
#define UNICODE
#include <windows.h>
int displayContent(std::wstring const &path, int level = 0) {
WIN32_FIND_DATA FileData;
if (path.length() > MAX_PATH)
return -1;
std::wstring new_path = path + L"\\*.*";
HANDLE hFile = FindFirstFile(new_path.c_str(), &FileData);
do {
if ((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (FileData.cFileName[0] == L'.'))
continue;
std::wcout << std::wstring(level, L'-') << L">" << FileData.cFileName << L"\n";
if (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
displayContent(path + L"\\" + FileData.cFileName, level + 2);
} while (FindNextFile(hFile, &FileData));
FindClose(hFile);
return 0;
}
int main(int argc, char* argv[]) {
wchar_t current_dir[MAX_PATH];
GetCurrentDirectory(sizeof(current_dir), current_dir);
displayContent(current_dir);
return 0;
}
[Note: I've also changed it to start from the current directory instead of always starting at the root of the C drive, but if you want to change it back, that's pretty trivial--in fact, it simplifies the code a bit more).
I am an absolute newbie to C++ and have only started to program with it 3 days ago.
I am trying to do the folliwng:
traverse a directory for X.X files (typically .), and for each file, do the following:
Search within the file for a string (findFirst) and then search until another string (findLast) - The files will be HTML format.
In this selection, I want to perform several tasks (yet to write) - but they will be the following:
One of the strings will be the Filename I want to write to. - so extract this field and create an outputfile with this name
Some of the lines will be manufacturer part numbers - extract these and format the output file accordingly
most of it will be description of product. Again - this will be in an HTML construct - so extract this and format the output file.
So far, I have managed to get working the traverse directory, and selecting the start and finish keywords - using some help from the internet.
My problem is here
processFiles(inputFileName, "testing", "finish");
I need the inputFileName to be the name of the traversed filename.
All the examples I have found simply print the filename using cout
I need to pass this into the processFiles function.
Can someone tell me what i need to use? i have tried it->c_Str() and other variations of (*it) and .at, .begin etc
my non printing example is below:
// 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();
}
Make your processFile accept a wstring, viz:
void processFiles(wstring inFileName, std::string findFirst,std::string findLast )
{
// Make the necessary changes so that you use a wstring for inFileName
}
Call it from main() using:
processFiles(*it, "testing", "finish");
You need to change processFile to use a wifstream instead of a ifstream and you should change all of your narrow strings to use wide strings (or vice versa). Narrow strings and wide strings are not compatible with each other and in order to use one with the other a conversion function must be used such as mbstowcs.
Edit:
You can find an example that should compile here.