C ++如何传递命令行参数以读取txt文件 [英] C++ How to pass command line argument to read txt file

查看:232
本文介绍了C ++如何传递命令行参数以读取txt文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试做的是...



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



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



是两个txt文件,其中一个命名为character.txt和其他match.txt。



文件的内容将是这样。



character.txt

 行。每行有两个字符串值
悟空Saiyan
Gohan Half_Saiyan
Kuririn人类
Piccolo名字
Frieza小人
细胞小人



match.txt

  //这包括三行,每行都是一个字符串
Goku Piccolo
Gohan Cell
Kuririn Frieza

如果我使用这些字符串而不使用命令行,我会这样在字符串中声明字符串。

  typedef字符串名称; //例如。 Goku 
typedef字符串类型; //例如。 Saiyan,Human等

现在我正在寻找如何读取和发送字符串值从txt文件像上面的那些,并且使用它们在main方法内的函数,理想地是这样的。

  int main ,char * argv)
{
for(int i = 1; i ,至少我理解可以使用命令行参数C ++,但我找不到任何更多的信息。如果您对此有任何建议,我们将不胜感激。



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

解决方案

由于你只是想读取文件的内容并处理它,从这个代码开始(没有任何错误检查tho)。它只是从命令行获取文件名,并将文件内容读入2个向量。然后你可以只处理这些向量作为u需要。

  #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>线;
std :: string line;
while(std :: getline(source,line))
{
lines.push_back(line);
}
返回行;
}

void displayVector(const std :: vector< std :: string&> v)
{
for(int i(0) = v.size(); ++ i)
std :: cout<< \\\
< 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);
}


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

1) to read txt files by command line argument,

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

For example, there are two txt files, one of which is named character.txt and the other 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.

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.

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.

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.

PS. I'm using Windows and Code Blocks.

解决方案

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.

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

这篇关于C ++如何传递命令行参数以读取txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆