Jump to content
  • Advertisement
Sign in to follow this  
Lonefox1

getting certain data from a text file

This topic is 4820 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys, currently making a rpg type game and most of its working but i need a way of getting random monster names and values of that monster into the constructor so its a bit more random. Im trying to use the fstream from a txt file like so:
string monstername;
            int monsterhealth;
            int monsterstr;
            int monsterexp;
            int monstergold;
            
            ifstream fin("monsters.txt");
            fin >> monstername >> monsterhealth >> monsterstr >> monsterexp >> monstergold;
            Monster mons(monstername,monsterhealth,monsterstr,monsterexp,monstergold);

all works well with one line of the text which is formated like so: Goblin 20 2 100 100 now lets say i wanted to use a random number to pick a random line out of the text file. i prefix my monsters in my text file so i can search for something like so: 1.Goblin 20 2 100 100 2.bunny 20 4 100 100 3.foo 20 20 100 100 would there be a way to search for "2." then start reading from the file? cheers lone.

Share this post


Link to post
Share on other sites
Advertisement
There are various (more or less efficient) ways of doing this directly from a file. But in this case it's probably a better idea to parse the file on startup and save the lines into a list of monster structures instead.

Share this post


Link to post
Share on other sites
umm.. by monster structures do you mean an array of monster instances? :| what exactly is a structure and how do i declare one?

any help appreciated
lone

Share this post


Link to post
Share on other sites
Quote:
Original post by Lonefox1
umm.. by monster structures do you mean an array of monster instances? :| what exactly is a structure and how do i declare one?
Yes, a list of monster instances. But in this case it'd probably be less confusing to refer to them as monster templates or types instead.

A structure is basically just a collection of variables.
struct monster {
const char *name;
int health;
int strength;
int experience;
int gold;
};
Then you can use store a bunch of them in an array (or some other datastructure) and retrieve the right one by its index.

Here's an example using a static array:
const monster monsterTypes[] = {
{ "first", 100, 50, 10, 250 },
{ "second", 200, 100, 20, 500 }
};

enum {
monsterCount = sizeof(monsterTypes) / sizeof(monsterTypes[0])
};

const monster *randomMonster() {
return &monsterTypes[rand() % monsterCount];
}
In your case you'd probably want to use a dynamic array, std::vector for example, instead. That way you can simply parse the file line-by-line and add the monsters to your list.

Share this post


Link to post
Share on other sites
a few questions, where would i declare a struct? i do have a monster class which has functions in it aswell. would i declare it in main, or would i change my monster class to a monster struct? also what would happen to its functions? would it store them in each instance too?

any help appreciated im rather confused with this one!

lone

Share this post


Link to post
Share on other sites
Your monster class, when written to a file, will just contain the fields, none of the functions, so it should behave just like a struct.

Share this post


Link to post
Share on other sites
In C++, a struct is the same as a class, just with default public visibility. In fact, structs can have member functions as well as variables. It's just that most people use classes because it makes things look nicer, and struct looks more C-like.

You would declare structs and classes in a header file.

Share this post


Link to post
Share on other sites
argh!! this is driving me nuts ive been trying to impliment this for the past 3 hours and i havnt a clue how to do it, can someone give me some hints or sumthing? i need to read the values into a structure from a text file containing a name and a few integers for each line as mentioned above. or any other suggestions how to obtain a similar sort of thing would be great too!

any help really appreciated!!!

lone

Share this post


Link to post
Share on other sites
You don't need to use a structure if you don't want to (especially if you've never worked with them).

Here's one trivial way you could do it:

//inside the text file:

#FILE1 Monster 20 34 20
#FILE2 Bunny 23 32 43
#FILE3 Goblin 32 43 54

//

What you do is make a function that'll read in a character at a time from the file and once it encounters '#', the function then reads in 'F''I''L''E' (as a string or character by character) and saves it into a buffer/array etc., Then you call a function to compare ( strcmp() ) "#FILE1" with the string you just took in and if they're both the same, then you offset however many bytes (characters) there is following " Monster " , in this case, it'll be 9 (7 for monster and 2 for the space before and after the name monster. You then you read in the value 20, 34, 20 etc... If the two strings being compared are'nt the same, then you just continue reading in the next character following "#FILE1". This way, you don't have to read in the whole file into your program [wink]

I hope you get what I'm trying to say. I did it this way once when I build my first databes entry program a long time ago and it worked very good.

Share this post


Link to post
Share on other sites
i had a brain wave :P finally got it working after much frustration.. or atleast the reading it in part. just wanted to run it past you guys to see if im missing any huge mistakes which will bite me on the bum in the future


//monster header file for struct
//monster.h header file

#ifndef MONSTER_H
#define MONSTER_H
#include <fstream>
#include <vector>
#include <iostream>
#include <string>


struct monster {
char name[20];
int health;
int strength;
int experience;
int gold;

}monsters[10];

#endif





//using it
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include "monster.h"

using namespace std;
int main()
{
//Creates a load of monsters-----------------
monster monsterTypes; //new monster struct

ifstream getit("iotest.txt"); //new ifstream
if (getit.bad()) //this doesnt work?
{
cerr << "couldnt find file\n";

}

for (int i = 0; i < 6; i++) //for get first 6 in files
{
getit >> monsters.name;
getit >> monsters.health;
getit >> monsters.strength;
getit >> monsters.experience;
getit >> monsters.gold;
}
for (int i = 0; i < 6; i++) //output to screen
{
cout << monsters.name << "\n";
cout << monsters.health << "\n";
cout << monsters.strength << "\n";
cout << monsters.experience << "\n";
cout << monsters.gold << "\n";
}
//---------------------------------------------

return 0;

}




one thing i havnt figured out yet is how to change the array size depending on how many entrys are in the txt file, any suggestions for this would be great

cheers
lone

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!