如何在C++中通过命令行参数读取txt文件

3
我一直试图做的是:
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文件中读取和发送字符串值,并将它们用于主方法中的函数,最理想的方式是这样。
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. 
    }
}

理想情况下,我希望以这种方式调用该方法。 enter image description here

根据这个网站的说法,至少我了解到在C++中可以使用命令行参数,但我找不到更多信息。如果您能提供任何建议,我将不胜感激。
附注:我正在使用Windows和Code Blocks。
5个回答

3

假设您只想读取文件的内容并进行处理,您可以从以下代码开始(没有任何错误检查)。它简单地从命令行获取文件名,并将文件内容读入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一些关于错误检查的警告是个好主意。原因有几个:回答完整性和更少的“我现在做错了什么?”后续问题和评论。 - user4581301

2

首先,主函数的原型应该是:

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

2

0

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

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


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;

 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接