Take this file for instance:
weapon.txt
knife
1
2
shortsword
2
5
longsword
5
8(end of file)
Normally when I want to grab data from the file I do this:
Code: Select all
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string temp; //Just a dump for data I don't need
string weapName;
int minDamage, maxDamage;
ifstream file("weapon.txt");
file >> temp;
file >> temp;
file >> temp;
file >> temp;
file >> temp;
file >> temp;
file >> weapName;(longsword)
file >> minDamage;(5)
file >> maxDamage;(8)
file.close();
return 0;
}
to store in variables to use.
My question is if there is any better way to do this so
I dont have to make so many "file >>"? It gets annoying
when the file happens to get very big.
EDIT: I thought about maybe storing it in an STL container
then extracting from there but I am not sure which container
would be best...something to label the names of each weapon,
its min and max damage then just extract them into their
correct variables.