File I/O

I would like to import a text file with weapon data for an RPG.

I was thinking I could read the file in the class constructor. It works but I am not sure how to pull specific lines from the text file as I may need them.

Plus there is issue that the data imported data that is a string. Makes it difficult to calculate damage. Would I just convert the string?
closed account (o3hC5Di1)
Hi there,

You may want to consider just storing objects of the weapons in a file rather than the data.
This has the advantage of not having to run through the file, decorating the objects as you read through it.

You will need to write / read in binary mode and you can use sizeof() to read an entire object at once.

Example:

1
2
3
4
5
ifstream ifs;
ifs.open("weapons.dat", ios::binary);

Weapon laser_cannon;
ifs.read(reinterpret_cast<char*>(laser_cannon), sizeof(Weapon));


Hope that's not too complicated for you - but I would think this is more effective than storing the data in a plain text file. It also limits users with no programming knowledge of tampering with the weapons data, it's not exactly security, but at least it hides the implementation a bit.

Let us know if you have any further questions.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.