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

C++ How to pass command line argument to read txt file

我一直在努力做的是...

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

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

比如有两个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++ 中使用命令行参数,但我找不到更多信息。如果您能就此提出任何建议,我将不胜感激。

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

要了解如何使用命令行参数请看这里。

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

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

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

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

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

如果您正好需要两个参数,您可以检查 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;
}

如果您希望有未指定数量的文件,那么您需要一个循环和一个数组来保存它们,即 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;
}

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

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

首先main函数原型应该是

    int main(int argc, char **argv)

    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;
     }
#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
    
    FILE *fp = fopen( argv[1], "r");
    char line[50];
   
     
    if (fp == NULL)
    {
        printf("File opening Unsuccessful\n");
        exit(1);
    }
    while (fgets(line , 30 , fp) != NULL)
    {
   
        printf("%s",line);
        
    }
   fclose(fp) ;
   return 0;

 }