Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading objects into array from file and vice versa

I'm currently trying to write a program which can read objects from a file into an array. Towards the end of the program I then want it to write out the contents of the array into the file. I have had some level of success with it so far, my reading from file method seems to work with no issues and to a degree I know I am close with my write to file method. It works, but it also outputs the array's new elements made by the default constructor. Is there any way I can prevent these default objects being written to file or better yet, stop them being made in the first place?

Here are my member variables, default constructor and methods in my class

private:
    //Member variables
    string stockCode;
    string stockDesc;
    int currentLevel;
    int reorderLevel;

//Defining Default Constructor
Stock::Stock()
{

}

//Defining function for items to file
void Stock::writeToFile(ofstream& fileOut)
{
    fileOut << stockCode << " ";
    fileOut << stockDesc << " ";
    fileOut << currentLevel << " ";
    fileOut << reorderLevel << " ";
}

//Defining function for reading items in from the file
void Stock::readFromFile(ifstream& fileIn)
{
        fileIn >> stockCode;
        fileIn >> stockDesc;
        fileIn >> currentLevel;
        fileIn >> reorderLevel;
}

And here is my main

#include <iostream>
#include <string>
#include <fstream>
#include "Stock.h"

using namespace std;

int main()
{   
    const int N = 15;
    Stock items[N];
    int option = 0;
    ifstream fileIn;
    fileIn.open("Stock.txt");
    for (int i = 0; i < N; ++i)
        items[i].readFromFile(fileIn);
    fileIn.close();
    cout << "1.Display full stock list." << endl;
    cout << "9.Quit." << endl;
    cout << "Please pick an option: ";
    cin >> option;
    switch (option)
    {
    case 1:
    {
        cout << "stockCode" << '\t' << "stockDesc" << '\t' << '\t' << "CurrentLevel" << '\t' << "ReorderLevel" << endl;
        cout << "------------------------------------------------------------------------------" << endl;
        for (int i = 0; i < N; ++i)
        {
            cout << items[i].getCode() << '\t' << '\t';
            cout << items[i].getDescription() << '\t' << '\t' << '\t';
            cout << items[i].getCurrentLevel() << '\t' << '\t';
            cout << items[i].getReorderLevel() << endl;
        }
        break;
    }

    case 9:
        ofstream fileOut;
        fileOut.open("Stock.txt");
        for (int i = 0; i < N; ++i)
        {
            items[i].writeToFile(fileOut);
        }
        break;
    }
    return 0;
}
like image 350
StonerLoods Avatar asked Feb 16 '26 12:02

StonerLoods


1 Answers

You could keep track of the number of items stored in the file, and eventually added in the code by storing that number as the first value in the file:

int num_items = 0;
// keep track of number of items in code
// when writing to the file, first write num_items
fileOut << num_items << " ";

When reading in, first read num_items:

fileIn >> num_items;

Then, instead of an array, use a std::vector to store and add the elements (you could set it's size to num_items up front to prevent reallocation, using reserve()).

like image 164
Danny_ds Avatar answered Feb 18 '26 14:02

Danny_ds