2013-10-27 53 views
0

我有一個文本文件 - > info.txt包含後續的標識,遊戲,量文本文件內容陣列

info.txt

1,left4dead,50 
2,warcraft3,60 

我知道如何使用向量來提取細節我的代碼如下所示。

stock.h

#ifndef stock_stock_h 
#define stock_stock_h 
#include <iostream> 

class stock {  
public: 
    stock() { 
    columnOneText = " "; 
    columnTwoText = " "; 
} 

stock(std::string columnOneText, 
     std::string columnTwoText 
); 

std::string getColumnOne(); 
std::string getColumnTwo(); 

void setItemId(std::string columnOneText); 
void setItemDescription(std::string columnTwoText); 

private: 
    std::string columnOneText, columnTwoText; 
};  
#endif 

的main.cpp

#include "stock.h" 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <sstream> 
#include <vector> 
using namespace std; 
stock::stock(string columnOneText, 
     string columnTwoText) 
{ 
    setItemId(columnOneText); 
    setItemDescription(columnTwoText); 
}; 
string stock::getColumnOne() { 
    return columnOneText; 
} 
string stock::getColumnTwo() { 
    return columnTwoText; 
} 
void stock::setItemId(string columnOneText) { 
    this->columnOneText = columnOneText; 
} 
void stock::setItemDescription(std::string columnTwoText) { 
    this->columnTwoText = columnTwoText; 
} 

int main(){ 
    vector<stock> itemDetails; 
    string line; 
    string columnOneText; 
    string columnTwoText; 
    ifstream readFile("info.txt"); 

    while(getline(readFile,line)) { 
    stringstream iss(line); 
    getline(iss, columnOneText,','); 
    getline(iss, columnTwoText, ','); 

    //consturctor 
    stock splitedColumns(columnOneText, 
         columnTwoText 
         ); 
    itemDetails.push_back(splitedColumns); 
} 
readFile.close(); 

cout << "this is column One in text file" << endl; 
for (int i =0; i<itemDetails.size(); i++) { 
    cout << itemDetails[i].getColumnOne() << " " << endl; 
} 

cout << "this is column Two in text file" << endl; 
for (int i =0; i<itemDetails.size(); i++) { 
    cout << itemDetails[i].getColumnTwo() << " " << endl; 
} 

}

什麼,我真的不知道是怎麼使用數組,而不是使用向量來提取細節。

我都試過,但它似乎沒有工作

string line; 
    string columnOneText; 
    string columnTwoText; 
    string columnOneTextArray[7]; 
    while(getline(readFile,line)) { 
    stringstream iss(line); 
    getline(iss, columnOneText,','); 
    getline(iss, columnTwoText, ','); 
    for(int i=0; i<8; i++) { 
     columnOneTextArray[i] = columnOneText; 
     cout << columnOneTextArray[i]; 
    } 
} 

你們可能會問爲什麼我想這樣做的陣列,而不是使用矢量, 我只是好奇和探索如何將它用數組完成。

+1

反向好奇心是合理的,不是這個 – P0W

+0

你有一個向量itemDetails。如果你想要數組而不是向量,那麼有一個itemDetails數組是合理的,而不是一個數組列一個文本。 –

回答

0

您的問題可以在很多簡單的方式通過除了使用struct定義來解決使用向量(你的類使用是有點矯枉過正,恕我直言):

#include <fstream> 
#include <iostream> 
#include <cstdlib> 
#include <vector> 
#include <sstream> 

//this utilizes a more idiomatic naming convention as opposed to `columnX` 
typedef struct Stock { 
    int id; 
    std::string games; 
    int amount; 
} Stock; 

//Syntactic sugar for easier output 
std::ostream& operator << (std::ostream& in, const Stock& stock) { 
    in << stock.id << " " << stock.games << " " << stock.amount; 
    return in; 
} 

int main() { 
    std::string filename = "info.txt"; 
    std::fstream ifile; 
    std::vector <Stock> inventory; 
    Stock stock_t;//a temporary Stock variable 
    std::string temp; 

    ifile.open(filename.c_str(), std::ios::in); 

    if (!ifile.is_open()) { 
     std::cerr << "There was a problem opening the input file: \"" << filename << "\"\n"; 
     exit(1); 
    } 

    while (ifile >> temp) { 
     std::istringstream iss(temp); 

     int column = 0; 
      //break each constituent line by using the comment as a delimiter: 
     while(std::getline(iss, temp, ',')) { 

      if (column == 0) { 
       stock_t.id = std::atoi(temp.c_str()); 
      } 
      else if (column == 1) { 
       stock_t.games = temp; 
      } 
      else if (column == 2) { 
       stock_t.amount = std::atoi(temp.c_str()); 
      } 

      ++column; 
     } 
     inventory.push_back(stock_t); 
    } 

    for (int i = 0; i < inventory.size(); ++i) { 
     std::cout << inventory[i] << "\n"; 
    } 

    ifile.close(); 

    return 0; 
} 

,輸出:

1 left4dead 50 
2 warcraft3 60