fstream questions

is there a cleaner way of achieving this

1
2
3
4
5
6
7
8
9
10
	std::ifstream subfile(test.txt, std::ios::in); 
	std::string s1, s2, s3, s4;
	bool b1, b2, b3, b4, b5;
	float f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15;
	if ( subfile.is_open() )
    {
		subfile >> s1 >> s2 >> s3 >> s4 >> b1 >> b2 >> b3 >> b4 >> b5 >> f1 >> f2 >> f3 >> f4 >> f5 >> f6 >> f7 >> f8 >> f9 >> f10 >> f11 >> f12 >> f13 >> f14 >> f15;
		TestData* td = new TestData(s1, s2, s3, s4, b1, b2, b3, b4, b5, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, f13, f14, f15);
	}
	subfile.close();


i have the files saved so all the data is ready to load in the proper order. this just seems like a long way of doing things. Is there a better way I don't know about?
Do you know how to use arrays?
Store all those repetitively named variables in an array and use a loop.
yea not really the answers i was hoping for tho. i was hoping there was a better way to use the Fstream system to load those directly into the TestData object. good points on the array i just wasn't using my head on that one.
You could define an operator>>() that reads from an ifsteam into your TestData class so you don't have to read into temporary variables then transfer the data.
Zhuge.. example?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Example {
    int data1;
    char data2;
// etc.
    friend istream& operator>>(ifstream& in, Example& rhs);
};

istream& operator>>(ifstream& in, Example& rhs) {
    in >> rhs.data1 >> rhs.data2;
    return in;
}

int main() {
     istream file;
     // do stuff to file
     Example foo;
     file>>foo;
     return 0;
}

Or something like that.
Topic archived. No new replies allowed.