【问题标题】:C++ How to pass command line argument to read txt fileC ++如何传递命令行参数来读取txt文件
【发布时间】: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

ma​​tch.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


    【解决方案1】:

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

    【讨论】:

    • 给 OP 一些关于在哪里检查错误的警告可能是个好主意。几个原因:回答的完整性和更少的“我现在做错了什么?”后续问题和 cmets。
    【解决方案2】:

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

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

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

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

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

    【讨论】:

      【解决方案3】:

      首先应该是主函数原型

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

      【讨论】:

        【解决方案4】:

        您错误地定义了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;
        }
        

        别忘了检查文件是否可以打开。

        【讨论】:

          【解决方案5】:
          #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;
          
           }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-09-28
            • 2021-10-29
            • 2016-06-09
            • 2017-03-18
            • 2013-05-27
            • 1970-01-01
            相关资源
            最近更新 更多