从文本文档中输入字符串作为参数

C++ Feeding strings from a text document as arguments

本文关键字:参数 字符串 输入 文本 文档      更新时间:2023-10-16

是否有办法直接从文本文档作为参数提供字符串?最好不要救他们?

我有一个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;
}