Beginner questions about File I/O

So I have a few questions, just trying to get my head around this file business:

- Is there a more direct way to assign the data from the file to the new object (ie. omit the need for Data[][])?
- I've created an array of three objects in this program. What happens if I want to delete pItem[1] and keep the rest?
- If I need specific data for Item "Pencil", which is hypothetically located at line XXX of items.txt, how do I grab that data and ignore the rest?
- In general, should I be opening/closing the files in any way other than how I am now? I've see while(!fItems.eof()) used a lot, so.

Super low-level questions here I'm guessing, this is my first shot at doing this. Thanks in advance guys.


Here's my code:
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 <fstream>

    using namespace std;

    class Item
    {
        public:
            void set_Stats(int a, int b, int c) { Quant = a; Power = b; Value = c; }
            int get_Quant() const { return Quant; }
            int get_Power() const { return Power; }
            int get_Value() const { return Value; }
        private:
        int Quant, Power, Value;
    };


    int main ()
    {
        const int AllStats = 3, ItemTotal = 3;
        int Data[ItemTotal][AllStats];
        Item *pItem = new Item[ItemTotal];
        ifstream fItems("items.txt");

        if(fItems.is_open())
        {
            for(int j = 0; j < ItemTotal; j++)
            {
                for(int i = 0; i < AllStats; i++)
                {
                    fItems >> Data[j][i];
                }

                pItem[j].set_Stats(Data[j][0], Data[j][1], Data[j][2]);

                cout << "\n\tItem " << j+1 << ":";
                cout << "\n\tQuant: " << pItem[j].get_Quant();
                cout << "\n\tPower: " << pItem[j].get_Power();
                cout << "\n\tValue: " << pItem[j].get_Value();
                cout << "\n";
            }

            fItems.close();
        }

        delete[] pItem;
        return 0;
    }

Item 1:
Quant: 99
Power: 50
Value: 250

Item 2:
Quant: 99
Power: 65
Value: 400

Item 3:
Quant: 24
Power: 90
Value: 750

items.txt
99 50 250
99 65 400
24 90 750
- Is there a more direct way to assign the data from the file to the new object (ie. omit the need for Data[][])?

Yes, you could give your class Item an operator>>, which then would allow

1
2
while(fItems >> item)
    items.push_back(item);

or even

1
2
istream_iterator<Item> beg(fItems), end;
vector<Item> items(beg, end);


- I've created an array of three objects in this program. What happens if I want to delete pItem[1] and keep the rest?

Use vectors, not arrays.

- If I need specific data for Item "Pencil", which is hypothetically located at line XXX of items.txt, how do I grab that data and ignore the rest?

Unless you've already read through the entire file at one point and cached the byte offsets of the beginning of each line, there is no way to jump to a specific line. You will have to read the file line by line (e.g. with while(getline(file, line)))

- In general, should I be opening/closing the files in any way other than how I am now?

Yes. Remove that fItems.close(); unless you have something special to do if close() fails. Files are unconditionally closed when they leave scope.

With proper file input, the is_open() check is not needed either, unless you have a non-empty "else" clause (which you don't).

I've see while(!fItems.eof()) used a lot, so.

That is (almost always) an error, don't use it.
Last edited on
Thanks for that, you cleared up a lot of what I needed to know. I'll see what I can do with the vector / operator function advice.
There really is no need for the array int Data[][] at all. All you need is three int variables to read each data item into:
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>

using namespace std;

class Item
{
public:
	void set_Stats(int a, int b, int c)
	{
		Quant = a;
		Power = b;
		Value = c;
	}
	int get_Quant() const
	{
		return Quant;
	}
	int get_Power() const
	{
		return Power;
	}
	int get_Value() const
	{
		return Value;
	}
private:
	int Quant, Power, Value;
};

int main()
{
	const int AllStats = 3, ItemTotal = 3;

	Item *pItem = new Item[ItemTotal];
	ifstream fItems("items.txt");

	if(fItems.is_open())
	{
		int Quant, Power, Value; // temp variables to read data into

		for(int j = 0; j < ItemTotal; j++)
		{
			for(int i = 0; i < AllStats; i++)
			{
				fItems >> Quant >> Power >> Value;
			}

			pItem[j].set_Stats(Quant, Power, Value);

			cout << "\n\tItem " << j + 1 << ":";
			cout << "\n\tQuant: " << pItem[j].get_Quant();
			cout << "\n\tPower: " << pItem[j].get_Power();
			cout << "\n\tValue: " << pItem[j].get_Value();
			cout << "\n";
		}

		fItems.close();
	}

	delete[] pItem;
	return 0;
}
Last edited on
Topic archived. No new replies allowed.