2015-05-09 542 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; 
    }