2015-05-09 550 views
3

我一直试图做的是...C++如何通过命令行参数读取TXT文件

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

According to this web site.,至少我明白可以在C++中使用命令行参数,但是我找不到更多的信息。如果您对此提出任何建议,我将不胜感激。

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

回答

2

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

给OP提供一些关于在哪里检查错误的警告可能是一个好主意。几个原因:回答完整性和更少“我现在做错了什么?”后续问题和意见。 – user4581301

1

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

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

1

首先,主函数原型应该是

int main(int argc, char **argv) 

OR

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