文本文件内容到数组

时间:2013-10-27 06:11:36

标签: c++ arrays csv file-io vector

我有一个文本文件 - > info.txt,其中包含以下ID,游戏,金额

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

你们可能会问我为什么要在数组中而不是使用vector来实现它, 我只是好奇并探索如何使用数组来完成它。

1 个答案:

答案 0 :(得分: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