سؤال

My program reads in a file that is a list of program names followed by a comma and list of command line arguments for that program. I want to store each line separately so that I can sort them and then later run them. The problem I am having is trying to wrap my head around what to store the information as. I thought to make a struct or class but I don't know what the command line arguments will be or how many of them there will be. For example the file's first line might be notepad.exe, test.txt and the next line may be myprogram.exe, 10 2000. what would be the best way to store an unknown number of arguements with an unknown data type?

Thanks

هل كانت مفيدة؟

المحلول

You can store each app name as the key of a std::map and the arguments as a std::vector

std::map<std::string, std::vector<std::string>> apps;
std::ifstream infile("apps.txt");

std::string line;
while (std::getline(infile, line))
{
    std::string appName = getEverythingBeforeComma(line);
    std::vector<std::string> arguments = getEverythingAfterCommaAndSplitBySpace(line);

    apps[appName] = arguments;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top