C++ C++;如何传递命令行参数以读取txt文件
我一直想做的是 1) 要通过命令行参数读取txt文件 2) 使用txt文件中的字符串作为main方法(或需要调用的任何方法)的参数 例如,有两个txt文件,其中一个名为character.txt,另一个名为match.txt 文件的内容如下所示C++ C++;如何传递命令行参数以读取txt文件,c++,command-line-arguments,C++,Command Line Arguments,我一直想做的是 1) 要通过命令行参数读取txt文件 2) 使用txt文件中的字符串作为main方法(或需要调用的任何方法)的参数 例如,有两个txt文件,其中一个名为character.txt,另一个名为match.txt 文件的内容如下所示 typedef string name; //e.g. Goku typedef string type; //e.g. Saiyan, Human, etc character.txt //This comprises of six rows. Ea
typedef string name; //e.g. Goku
typedef string type; //e.g. Saiyan, Human, etc
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.
}
}
intmain(intargc,char*argv)
{
对于(int i=1;i cout要了解如何使用命令行参数,请查看以下内容
无法使用通过命令行参数传递给应用程序的文件内容。只有文件名被传递给应用程序
您应该使用该名称打开文件并读取其内容。请查看以下内容:
您错误地定义了main
原型。您还需要读取文件
如果只需要两个参数,可以选中argc
并直接提取参数:
int main(int argc, char* argv[]) {
if(argc != 3) {
std::cerr << "Usage: " << argv[0]
<< " name.txt match.txt" << std::endl;
return 1;
}
std::ifstream name_file(argv[1]);
std::ifstream match_file(argv[2]);
// ...
return 0;
}
不要忘记检查文件是否可打开。如果您只想读取文件内容并对其进行处理,您可以从以下代码开始(无任何错误检查)。它只需从命令行获取文件名并将文件内容读取为2个向量。然后您可以根据需要处理这些向量
#include <string>
#include <fstream>
#include <iostream>
#include <vector>
std::vector<std::string> readFileToVector(const std::string& filename)
{
std::ifstream source;
source.open(filename);
std::vector<std::string> lines;
std::string line;
while (std::getline(source, line))
{
lines.push_back(line);
}
return lines;
}
void displayVector(const std::vector<std::string&> v)
{
for (int i(0); i != v.size(); ++i)
std::cout << "\n" << v[i];
}
int main(int argc, char **argv)
{
std::string charactersFilename(argv[1]);
std::string matchesFilename(argv[2]);
std::vector<std::string> characters = readFileToVector(charactersFilename);
std::vector<std::string> matches = readFileToVector(matchesFilename);
displayVector(characters);
displayVector(matches);
}
#包括
#包括
#包括
#包括
std::vector readFileToVector(常量std::字符串和文件名)
{
std::ifstream源;
开源(文件名);
std::矢量线;
std::字符串行;
while(std::getline(源,行))
{
线。推回(线);
}
回流线;
}
void displayVector(const std::vector v)
{
for(int i(0);i!=v.size();++i)
std::cout首先,主要功能原型应该是
int main(int argc, char **argv)
或
在主函数中检索文件名后,应打开每个文件并检索其内容
第三个示例代码
int main(int argc, char* argv[])
{
for(int i=1; i <= argc; i++) // i=1, assuming files arguments are right after the executable
{
string fn = argv[i]; //filename
cout << fn;
fstream f;
f.open(fn);
//your logic here
f.close();
}
return 0;
}
intmain(intargc,char*argv[])
{
对于(int i=1;我可能是一个好主意,给OP一些关于在哪里检查错误的警告。原因有两个:回答完整性和更少的“我现在做错了什么?”后续问题和评论。
int main(int argc, char* argv[])
{
for(int i=1; i <= argc; i++) // i=1, assuming files arguments are right after the executable
{
string fn = argv[i]; //filename
cout << fn;
fstream f;
f.open(fn);
//your logic here
f.close();
}
return 0;
}