从 C++ 中的文本文件中读取黑名单
Reading blacklist from a text file in C++
我实际上需要我的驱动程序读取(逐行)一些将被列入黑名单的程序。
_T("bannedfile.exe") 是我实际需要放置黑名单程序的地方。
如何让 _tcscmp 逐行读取文本文件?
(加载驱动的宿主程序与黑名单程序进行对比)
BOOL ProcessBlackList() {
TCHAR modulename[MAX_PATH];
GetModuleFileName(NULL, modulename, MAX_PATH);
PathStripPath(modulename);
if (_tcscmp(modulename, _T("bannedfile.exe")) != 1) {
return 0;
}
else {
return 0x2;
}
}
不能那样做。
您应该能够使用 getline 逐行读取文件,然后将这些行传递给 _tcscmp。应该像这样工作:
wchar_t const name[] = L"bannedfile.exe";
std::wifstream file(name);
std::wstring line;
while (std::getline(file, line)
{
if (_tcscmp(modulename, line.c_str()) == 0) {
return TRUE; //module is in list
}
}
return FALSE; // module is not in list
目前缺少用于测试的 VS 副本。
如果您 运行 遇到 unicode 解析问题,因为文件的编码与默认设置不完全相同,请阅读此内容:What is std::wifstream::getline doing to my wchar_t array? It's treated like a byte array after getline returns
我实际上需要我的驱动程序读取(逐行)一些将被列入黑名单的程序。
_T("bannedfile.exe") 是我实际需要放置黑名单程序的地方。
如何让 _tcscmp 逐行读取文本文件?
(加载驱动的宿主程序与黑名单程序进行对比)
BOOL ProcessBlackList() {
TCHAR modulename[MAX_PATH];
GetModuleFileName(NULL, modulename, MAX_PATH);
PathStripPath(modulename);
if (_tcscmp(modulename, _T("bannedfile.exe")) != 1) {
return 0;
}
else {
return 0x2;
}
}
不能那样做。
您应该能够使用 getline 逐行读取文件,然后将这些行传递给 _tcscmp。应该像这样工作:
wchar_t const name[] = L"bannedfile.exe";
std::wifstream file(name);
std::wstring line;
while (std::getline(file, line)
{
if (_tcscmp(modulename, line.c_str()) == 0) {
return TRUE; //module is in list
}
}
return FALSE; // module is not in list
目前缺少用于测试的 VS 副本。
如果您 运行 遇到 unicode 解析问题,因为文件的编码与默认设置不完全相同,请阅读此内容:What is std::wifstream::getline doing to my wchar_t array? It's treated like a byte array after getline returns