building i/o function to external file

hello everybody
i'm wondering how to make a function to make data input/outup to an external file. i do know only the basics on fstream.

i'm trying to make a little game, just as a hobby, and want it to have lots of different items. so, i was told that i shouldn't make every item hardcoded. instead, i should make an external file that contain every item, and then make the code load this file (is this called data-driven design?).

so, lets suppose i create an .txt file structured this way:

Item{
name=potion
value=30
}

Item{
name=sword
value=100
}

Item{
name=flower
value=1
}


and a function loadItem(), like this (coding errors probably included):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ItemClass{
  std::string name;
  int value;
};

std::vector item_list;

int loadIem(std::string name, std::vector &item_list){
  ItemClass new_sword;
  new_sword.name = ?
  new_sword.value = ?
  
  item_list->push_back(new_sword);

  return 0;
}


how to convert the members from the external file to object members? and how to convert from char(srings) in the file to int in the code?
should i use binary i/o?
and what is serialization?
and also, i'd not like to use external libraries, like boost, etc...

thanks in advance! :)
This is more a suggestion on your item list.

At some point you may have dozens of potions/swords/armors.
I would add to your list a item type, so Type=Potion, Type=Weapon, Type=Armor
Then you might want to to limit how much they can carry, one way to do that is to give each item a weight.
Another idea for inventory is that you might give each item a Item#
00100=potion1
00101=potion2
00200=Sword1 Iron
00201=Sword2 Steel
00203=Sword2 Silver

In addition, you can give each item type a special value showing where in your inventory it can be carried/used (in this example the first digit).
10100 Bag or backpack
20200 Weapon
30300 Armor

I would work on the item list before you put a lot of work into coding.
You may put your items all on one line, which I think would be easier
Item# Name Value Weight
10100 BluePotion 10 .2
10101 RedPotion 10 .2

If your names have spaces, then you might want to separate them with a marker, such as:

1
2
3
4
5
6
7
8
Item#  Name  Value  Weight
10100, Blue Potion, 10, .2
or
10101|Red Potion|20|.2
or with tabs

1234567890123456789012345678901234567890 - < numbers show spacing
10101    Red Potion         20        .2


This will make determine if the line was read correctly a lot easier, allow you to check that the item database is not corrupt.

Then figure out if you want to load the file into an array, show only the weapons or show everything available to purchase/sell.
Last edited on
thanks for the suggestions. yes, i do have more Item classes, for specific types. i tried just to make it simpler here in the forum.
the Item# is a good idea too. i'll make something like "Item ID". thanks for that :)

last time i made something similar, i did use the "|" character to separate things. but back then, i had only text in the file, not numbers. and everything was just for output on the console.
now, i need to be able to read text, numbers, bools, and put everything in a newly created object...
There are a couple ways to read the data, Here is a simple go at it, reading one line at a time and then using string.erase().

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;

main ()
{
	
//declare variables
ifstream inData;
string line;
string ItemNum;
string ItemName;
string ItemPrice;
string ItemWeight;


//open data file	
inData.open("ItemList.txt");
if (inData.is_open())
{
	while (inData.good())
	{
    getline(inData,line);	
	
	if (line.size() == 40)		// if line size is 40
		{
		ItemNum=line;
		ItemNum.erase(6,34);
		
		ItemName=line;
		ItemName.erase(0,9);
		ItemName.erase(15,16);
		
		ItemPrice=line;
		ItemPrice.erase(0,25);
		ItemPrice.erase(5,10);
		
		ItemWeight=line;
		ItemWeight.erase(0,35);

		cout << "Item#=\t" << ItemNum << "\n" <<  "Name =\t" << ItemName << "\n" << "Value=\t" << ItemPrice << "\n" << "Lbs  =\t" << ItemWeight << endl;
		}
	else
		{break;}
	}
}

inData.close();
return 0;
}


Example ItemList.txt

1
2
3
000010   Rough Gem          10         1
100010   Potion             10        .5
200010   Rusted Sword       20        20


You still have to deal with spaces and converting string to number.
Last edited on
yeah, and THAT'S the big point: how to convert from string to int, double, bool, etc?
spaces are no problem, we can do something like this in ItemList.txt:
#000010 <Rough Gem> <10> <1>
#100010 <Potion> <10> <.5>
#200010 <Rusted Sword> <20> <20>

and search for the < and > characters.

thanks again!
Last edited on
Try taking a look at this article and see if it helps.
http://www.cplusplus.com/articles/D9j2Nwbp/
sure it does! i'll try it right now! :)
thanks everybody!
Topic archived. No new replies allowed.