C++ 将文本文档中的字符串作为参数
C++ Feeding strings from a text document as arguments
有没有办法直接从文本文档中输入字符串作为参数?最好不要保存它们?
我有一个需要参数的 killProcessByName 方法,所以我想知道是否可以从我的文本文档中读取第一行,复制它然后将其作为参数发送?然后转到下一行,执行相同的操作并重复该过程,直到文档中没有留下任何单词?
我的列表示例:
Apples.exe
Blueberries.exe
Watermelon.exe
Oranges.exe
...
我的目标方法
void killProcessByName(const char *filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcasecmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
#include <iostream> // std::cout, std::endl
#include <fstream> // std::ifstream
using namespace std;
int main()
{
// open your file
ifstream input_file("test.txt");
// create variables
string name;
// while there are entries
while(input_file >> name)
{
killProcessByName(name.c_str());
}
// close the file
input_file.close();
return 0;
}
有没有办法直接从文本文档中输入字符串作为参数?最好不要保存它们?
我有一个需要参数的 killProcessByName 方法,所以我想知道是否可以从我的文本文档中读取第一行,复制它然后将其作为参数发送?然后转到下一行,执行相同的操作并重复该过程,直到文档中没有留下任何单词?
我的列表示例:
Apples.exe
Blueberries.exe
Watermelon.exe
Oranges.exe
...
我的目标方法
void killProcessByName(const char *filename)
{
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPALL, NULL);
PROCESSENTRY32 pEntry;
pEntry.dwSize = sizeof (pEntry);
BOOL hRes = Process32First(hSnapShot, &pEntry);
while (hRes)
{
if (strcasecmp(pEntry.szExeFile, filename) == 0)
{
HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, 0,
(DWORD) pEntry.th32ProcessID);
if (hProcess != NULL)
{
TerminateProcess(hProcess, 9);
CloseHandle(hProcess);
}
}
hRes = Process32Next(hSnapShot, &pEntry);
}
CloseHandle(hSnapShot);
}
#include <iostream> // std::cout, std::endl
#include <fstream> // std::ifstream
using namespace std;
int main()
{
// open your file
ifstream input_file("test.txt");
// create variables
string name;
// while there are entries
while(input_file >> name)
{
killProcessByName(name.c_str());
}
// close the file
input_file.close();
return 0;
}