简体   繁体   中英

text file contents to array

I have a text file-> info.txt which contains the follow Id,games,amount

info.txt

1,left4dead,50
2,warcraft3,60

I know how to extract the details using vectors with my codes shown below.

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

}

what I don't really know is how to extract the details using arrays instead of using vectors.

I have tried this but it doesn't seem to work

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

You guys might ask why do I want to do it in arrays instead of using vector, I just curious and exploring how it can be done using arrays.

You problem can be solved in a lot simpler manner by using struct definitions in addition to using vectors (your use of classes is a bit of overkill, IMHO):

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

Which outputs:

1 left4dead 50
2 warcraft3 60

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM