1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
// Open your pre-processor directives
std::string name, desc = ""; // Declare and initialize name and desc strings, assign them a blank value.
int id, weight, loc = 0; // Declare and initialize intigers id, weight, and loc, and give them the value zero.
std::ifstream thefile; // Declare inout file stream and name the handle 'thefile'
thefile.open("Itemdata.dat"); // Open the file Itemdata.dat for reading (your text file)
if (thefile.is_open()){ // Check to ensure file is open.
while (thefile.good()){
getline(thefile,name,'\}'); // Get the first line and assign it to the string name.
thefile.ignore(1,'\0');
getline(thefile,desc,'\}'); // Get the next line and assign it to the string desc.
thefile.ignore(1,'\0');
thefile >> id >> weight >> loc; // Get the next three integer values and assign them accordingly.
thefile.ignore(1,'\0');
// Here you add them to your container(s)
}
} // If file has not reached the end (still open) , repeat.
thefile.close(); // close the file
|