我一直试图做的是:
1)通过命令行参数读取txt文件,
2)将txt文件中的字符串作为主方法(或者你需要调用的任何方法)的参数使用。
例如,有两个txt文件,一个名为character.txt,另一个名为match.txt。
文件的内容会像这样: character.txt
如果我不使用命令行,而直接使用这些字符串,我会在 character.txt 文件中声明这些字符串,就像这样。
现在我正在寻找如何从类似上面的txt文件中读取和发送字符串值,并将它们用于主方法中的函数,最理想的方式是这样。
理想情况下,我希望以这种方式调用该方法。 根据这个网站的说法,至少我了解到在C++中可以使用命令行参数,但我找不到更多信息。如果您能提供任何建议,我将不胜感激。
附注:我正在使用Windows和Code Blocks。
1)通过命令行参数读取txt文件,
2)将txt文件中的字符串作为主方法(或者你需要调用的任何方法)的参数使用。
例如,有两个txt文件,一个名为character.txt,另一个名为match.txt。
文件的内容会像这样: character.txt
//This comprises of six rows. Each of the rows has two string values
Goku Saiyan
Gohan Half_Saiyan
Kuririn Human
Piccolo Namekian
Frieza villain
Cell villain
match.txt
//This comprises of three rows, each of them is one string value
Goku Piccolo
Gohan Cell
Kuririn Frieza
如果我不使用命令行,而直接使用这些字符串,我会在 character.txt 文件中声明这些字符串,就像这样。
typedef string name; //e.g. Goku
typedef string type; //e.g. Saiyan, Human, etc
现在我正在寻找如何从类似上面的txt文件中读取和发送字符串值,并将它们用于主方法中的函数,最理想的方式是这样。
int main(int argc, char *argv)
{
for (int i = 1; i < argc; i++) {
String name = *argv[i]; //e.g. Goku
String type = *argv[i]; //e.g. Saiyan, Human, etc
String match = * argv[i]; //Goku Piccolo
//I don't think any of the statements above would be correct.
//I'm just searching for how to use string values of txt files in such a way
cout << i << " " << endl; //I'd like to show names, types or matchs inside the double quotation mark.
}
}
理想情况下,我希望以这种方式调用该方法。 根据这个网站的说法,至少我了解到在C++中可以使用命令行参数,但我找不到更多信息。如果您能提供任何建议,我将不胜感激。
附注:我正在使用Windows和Code Blocks。