开发者

Read data from a file into a vector of objects

I'm trying to read data from a file(if there is any) into a vector of objects, but I get either no data or garbage data. I'm not super familiar with vectors, it hasn't been taught in class yet but I'm trying to use them anyway.

Here is where I create an object that I will use to push_back() objects into the inventory vector.

int main() {
    
    ifstream file;
    Clothes invdata;
    vector<Clothes>inventory;
    readFile(file, inventory, invdata);

In the readfile function, i try to open the file, and if it opens, check if the file is empty. If the file isn't empty, I want to read the data from the file into an object in the vector. The problem is that the data the is being read is garbage data and not what is in the text file.

void readFile(ifstream &f, vector<Clothes> &s,Clothes c) {开发者_C百科
    f.open("inventory.txt");
    if (f) {
        cout << "Inventory Data Loaded\n";
        f.seekg(0, std::ios::end);
        unsigned int fsize = f.tellg();
        if (!fsize) {
            cout << "Empty File\n";
            f.close();
            return;
        }
        int i = 0;
        string brand, type, color, size;
        double cost, saleprice;
        while (!f.eof()) {
            s.push_back(c);
            f >> brand >> type >> color >> size >> cost >> saleprice;
            s[i].setBrand(brand);
            cout << s[i].getBrand();
            s[i].setType(type);
            cout << s[i].getType();
            s[i].setColor(color);
            cout << s[i].getColor();
            s[i].setSize(size);
            cout << s[i].getSize();
            s[i].setCost(cost);
            cout << s[i].getCost();
            s[i].setSalePrice(saleprice);
            cout << s[i].getSalePrice();
            i++;
        }
        f.close();
    }
    else {
        f.close();
        cout << "Inventory Data not found, would you like to create a new file?" << endl;
        cout << "1) Create new file" << endl;
        cout << "2) Exit The Program" << endl;
        int a{ 0 };
        cin >> a;
        while (a != 1 && a != 2) {
            cout << "Error, please select a valid option" << endl;
            cin >> a;
        }
        if (a == 1) {
            fstream f;
            f.open("inventory.txt", std::ios::out);
            f << fflush;
            f.close();
            return;
        }
        else if (a == 2) {
            exit(0);
        }
    }
}

This is the text file that i'm trying to read from

Nike
Shoe
Black
13
88.88
100

and this is my class

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
#include <vector>
#include <iomanip>

using namespace std;

class Clothes {
private:
    string brand;
    string size;
    string type;
    string color;
    double cost, saleprice;

public:
    Clothes() {
        brand = "None";
        size = "None";
        type = "None";
        color = "None";
        cost = 0;
        saleprice = 0;
    }
    void setBrand(string b) {
        this->brand = b;
    }
    void setSize(string s) {
        this->size = s;
    }
    void setType(string t) {
        this->type = t;
    }
    void setColor(string c) {
        this->color = c;
    }
    void setCost(double c) {
        this->cost = c;
    }
    void setSalePrice(double p) {
        this->saleprice = p;
    }
    string getBrand() {
        return brand;
    }
    string getSize() {
        return brand;
    }
    string getType() {
        return type;
    }
    string getColor() {
        return color;
    }
    double getCost() {
        return cost;
    }
    double getSalePrice() {
        return saleprice;
    }
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜