Reading objects into array from file and vice versa

Hi all, 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, made in the first place?

Here are my member variables default constructor and methods in my class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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 this is my code in main .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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;
}
Last edited on
I don't understand what the problem is. Your code should work just fine.

Your stock class does not HAVE to have a constructor. You can just delete it.

http://stackoverflow.com/questions/13654927/why-explicitly-delete-the-constructor
Instead of reading/writing a set amount of times, you could keep track of the number of items successfully read from the file and write the same amount back to it.

As for not creating them at all, consider using a std::vector rather than an array.
When writing back to the array with a amount of items variable, would it be possible to leave a blank space at the end of the array? or is that not possible with object arrays?
When writing back to the array with a amount of items variable, would it be possible to leave a blank space at the end of the array?

I don't know what you mean.
You need to distinguish between the capacity of the array (the maximum number of items it can hold), and the number of items which are stored.

Maybe something like this:
1
2
3
4
5
6
7
8
    const int SIZE = 100;
    Stock items[SIZE];
    int n = 0;

    ifstream fileIn("Stock.txt");
    while ( (n < SIZE) && items[n].readFromFile(fileIn) )
        n++;
    fileIn.close();

For this to work, function readFromFile() needs to return a boolean value, true if the read was successful, false if it failed.

So can this be used to leave the remainder of the array blank or does every slot have to have an object in it?
So can this be used to leave the remainder of the array blank or does every slot have to have an object in it?

Every slot has an object in it. Again, I would direct you to std::vector if you want an array that can take on an arbitrary size.
Well, it looks like for what I've been tasked with doing, I most likely do need to use vectors, thank you everyone
Topic archived. No new replies allowed.