【发布时间】:2015-07-20 09:11:48
【问题描述】:
我一直在尝试做的是......
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文件中读取和发送字符串值,并将它们用于main方法中的函数,最好是这样。
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.
}
}
理想情况下,我想以这种方式调用此方法。
According to this web site.,至少我知道可以在 C++ 中使用命令行参数,但我找不到更多信息。如果您对此提出任何建议,我将不胜感激。
附言。我正在使用 Windows 和代码块。
【问题讨论】:
标签: c++ command-line-arguments