inFile issue

Simple program, compiles properly, yet I can't for the life of me figure out what's going on when I try load. The data doesn't input correctly.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//========================================
// Cube - Save/Load
//========================================

#include<iostream>
#include<fstream>
using namespace std;

struct Point3
{
	Point3();
	Point3(float x, float y, float z);

	void save(ofstream& outFile);
	void load(ifstream& inFile);
	void print();
	
	float mX;
	float mY;
	float mZ;
};

void Point3::save(ofstream& outFile)
{
	outFile << mX << mY << mZ;
}

void Point3::load(ifstream& inFile)
{
	inFile >> mX >> mY >> mZ;
}

Point3::Point3()
{
	mX = mY = mZ = 0.0f;
}

Point3::Point3(float x, float y, float z)
{
	mX = x;
	mY = y;
	mZ = z;
}

int main()
{
	Point3 cube[8];

	cube[0] = Point3(-1.0f, -1.0f, -1.0f);
	cube[1] = Point3(-1.0f, 1.0f, -1.0f);
	cube[2] = Point3(1.0f, 1.0f, -1.0f);
	cube[3] = Point3(1.0f, -1.0f, -1.0f);
	cube[4] = Point3(-1.0f, -1.0f, 1.0f);
	cube[5] = Point3(-1.0f, 1.0f, 1.0f);
	cube[6] = Point3(1.0f, 1.0f, 1.0f);
	cube[7] = Point3(1.0f, -1.0f, 1.0f);

	ofstream outFile("pointdata.txt");

	if (outFile)
	{	
		for (int i = 0; i < 8; ++i)
			cube[i].save(outFile);

		outFile.close();
	}
	
	cube[0] = Point3(0, 0, 0);
	cube[1] = Point3(0, 0, 0);
	cube[2] = Point3(0, 0, 0);
	cube[3] = Point3(0, 0, 0);
	cube[4] = Point3(0, 0, 0);
	cube[5] = Point3(0, 0, 0);
	cube[6] = Point3(0, 0, 0);
	cube[7] = Point3(0, 0, 0);
	
	cout << "BEFORE LOADING..." << endl << endl;

	for (int i = 0; i < 8; ++i)
	{
		cout << "cube[" << i << "] = ";
		cout << "(";
		cout << cube[i].mX << ", ";
		cout << cube[i].mY << ", ";
		cout << cube[i].mZ << ")" << endl;
	}

	ifstream inFile("pointdata.txt");

	if (inFile)
	{
		for (int i = 0; i < 8; ++i)
			cube[i].load(inFile);

		inFile.close();
	}

	cout << "\nAFTER LOADING..." << endl << endl;

	for (int i = 0; i < 8; ++i)
	{
		cout << "cube[" << i << "] = ";
		cout << "(";
		cout << cube[i].mX << ", ";
		cout << cube[i].mY << ", ";
		cout << cube[i].mZ << ")" << endl;
	}
	
	cout << "\n";
	system("pause");
}
Last edited on
Have you looked at the saved data in a text editor? I'm guessing you might want to put spaces between the saved coordinates...
Yeah, I looked in the saved data, it's all just one line. How exactly would I do I input the spaces though? Just use normal spaces in the save function between variables, and then add in a garbage variable to read over it in load?

Thanks for the prompt reply!
The extraction operator (>>) will use whitespace as a delimiter by default. Write the spaces between the values, cross your fingers, and it should load them just fine. ;)
Thank you very much moorecm, I cannot stress it enough. I followed my textbook word for word for this task, the least they could have told me was that!
Topic archived. No new replies allowed.