C ++如何将命令行参数传递给读取txt文件

时间:2020-12-29 19:25:24

What I've been trying to do is...

我一直想做的是......

1) to read txt files by command line argument,

1)通过命令行参数读取txt文件,

2) to use strings in the txt files as arguments for the main method (or whatever method you need to invoke).

2)在txt文件中使用字符串作为main方法的参数(或者你需要调用的任何方法)。

For example, there are two txt files, one of which is named character.txt and the other match.txt.

例如,有两个txt文件,其中一个名为character.txt,另一个名为match.txt。

The contents of the files would be like this.

文件的内容是这样的。

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

If I use those strings without using command line, I'd declare the strings in character.txt like this.

如果我在不使用命令行的情况下使用这些字符串,我会像这样在character.txt中声明字符串。

typedef string name; //e.g. Goku
typedef string type; //e.g. Saiyan, Human, etc

Now I'm looking for how to read and send string values from txt files like the ones above, and to use them for functions inside the main method, ideally like this way.

现在我正在寻找如何从上面的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. 
    }
}

Ideally, I'd like to invoke this method in this way. C ++如何将命令行参数传递给读取txt文件

理想情况下,我想以这种方式调用此方法。

According to this web site., at least I understand it is possible to use command line arguments with C++, but I cannot find any more information. I'd appreciate if you'd give any advice on it.

根据这个网站。至少我知道可以使用C ++的命令行参数,但我找不到更多的信息。如果您对此提出任何建议,我将不胜感激。

PS. I'm using Windows and Code Blocks.

PS。我正在使用Windows和代码块。

4 个解决方案

#1


Asuming you just want to read contents of the files and process it, you can start with this code (Without any errors checks tho). It simply gets filenames from command line and reads file contents into 2 vectors. Then you can just process these vectors as u need.

假设您只想读取文件的内容并进行处理,您可以从这个代码开始(没有任何错误检查)。它只是从命令行获取文件名,并将文件内容读入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);
}

#2


to see how to use command line arguments look at this.

看看如何使用命令行参数看看这个。

http://www.cplusplus.com/articles/DEN36Up4/

you cannot use the contents of the file which you have passed to your app through command line arguments. only the name of the file is passed to the app.

您不能使用通过命令行参数传递给应用程序的文件的内容。只有文件名传递给应用程序。

you should open the file using that name and read its contents. take a look at this:

您应该使用该名称打开文件并阅读其内容。看看这个:

http://www.cplusplus.com/doc/tutorial/files/

#3


You define main prototype incorrectly. You also need std::ifstream to read files.

您错误地定义了主要原型。您还需要std :: ifstream来读取文件。

If you expect exactly two arguments, you may check argc and extract arguments directly:

如果您只想要两个参数,可以直接检查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;
}

If you expect unspecified number of files, than you need a loop and an array to save them, i.e. vector:

如果你期望文件数量不确定,那么你需要一个循环和一个数组来保存它们,即vector:

int main(int argc, char* argv[]) {
    std::vector<std::ifstream> files;

    for(int i = 1; i < argc; ++i) 
        files.emplace_back(argv[i]);

    // ...

    return 0;
}

And do not forget to check if files are openable.

并且不要忘记检查文件是否可打开。

#4


First the main function prototype should be

首先应该是主要功能原型

    int main(int argc, char **argv)

OR

    int main(int argc, char *argv[])

Second after retrieving files names in the main function you should open each file and retrieve its contents

在main函数中检索文件名后,您应该打开每个文件并检索其内容

Third Sample code

第三个示例代码

    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;
     }

#1


Asuming you just want to read contents of the files and process it, you can start with this code (Without any errors checks tho). It simply gets filenames from command line and reads file contents into 2 vectors. Then you can just process these vectors as u need.

假设您只想读取文件的内容并进行处理,您可以从这个代码开始(没有任何错误检查)。它只是从命令行获取文件名,并将文件内容读入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);
}

#2


to see how to use command line arguments look at this.

看看如何使用命令行参数看看这个。

http://www.cplusplus.com/articles/DEN36Up4/

you cannot use the contents of the file which you have passed to your app through command line arguments. only the name of the file is passed to the app.

您不能使用通过命令行参数传递给应用程序的文件的内容。只有文件名传递给应用程序。

you should open the file using that name and read its contents. take a look at this:

您应该使用该名称打开文件并阅读其内容。看看这个:

http://www.cplusplus.com/doc/tutorial/files/

#3


You define main prototype incorrectly. You also need std::ifstream to read files.

您错误地定义了主要原型。您还需要std :: ifstream来读取文件。

If you expect exactly two arguments, you may check argc and extract arguments directly:

如果您只想要两个参数,可以直接检查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;
}

If you expect unspecified number of files, than you need a loop and an array to save them, i.e. vector:

如果你期望文件数量不确定,那么你需要一个循环和一个数组来保存它们,即vector:

int main(int argc, char* argv[]) {
    std::vector<std::ifstream> files;

    for(int i = 1; i < argc; ++i) 
        files.emplace_back(argv[i]);

    // ...

    return 0;
}

And do not forget to check if files are openable.

并且不要忘记检查文件是否可打开。

#4


First the main function prototype should be

首先应该是主要功能原型

    int main(int argc, char **argv)

OR

    int main(int argc, char *argv[])

Second after retrieving files names in the main function you should open each file and retrieve its contents

在main函数中检索文件名后,您应该打开每个文件并检索其内容

Third Sample code

第三个示例代码

    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;
     }