Creating a Game

Hello! I am a programmer that is new to the language of C++, and i am very confused about one thing. How do i read a specific part of a file? I am trying to create a game that reads the prices of an object inputted by the user from a file. This is the code i have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include <iostream>
 #include <fstream>
  using namespace std;

 int main () {

  ifstream infile;

  infile.open ("Objects.txt", ifstream::in);

  int ch = infile.get();
  while (infile.good()) {
    cout << (char) ch;
    ch = infile.get();
  }
 }



The file contains this :

Example objects
( ) store items
(item) (purchase value)/(sell value)

Grocery Store Items
Fish 5 7
Vegetables 10 15
Drinks 20 30

Weapon Store Items
Pistol 300 375
Rifle 400 500
Ammunition 20 30



Thanks for taking your time to read this and helping create this game with me!
I will mention your username in a comment in the code! :)
Last edited on
closed account (Dy7SLyTq)
first on line 9 its ios::in not ifstream::in second its useless because ifstream is that by default. third why not use std::string line
while(getline(infile, line))
{
cout<< line << endl;
}
If you already know the position in the file that you want to read from (or you want to know what the current position is), you can use istream::tellg() and istream::seekg() to respectively get and set the position within the file that you want to read next.

http://www.cplusplus.com/reference/istream/istream/tellg/
http://www.cplusplus.com/reference/istream/istream/seekg/

However, in this case what you want is to determine the parts of the file that are of interest. That happens through parsing the file's content and determining what is the relevant part by means of comparison with the surrounding entries.

E.g.

For the sample file you provide: if you keep iterating through the file's content, reading characters and storing them in a temporary string variable (and emptying that variable whenever you meet a space), you will be able to parse seperate "words", one at a time. Whenever a space is found and your string is not empty already, you can suppose that a complete word is formed, and you can compare that string variable with a list of recognizable keywords that your file-parser supports (E.g. "Grocery").

The above approach can be made to conditionally store more than a single word (if triggered by keywords that indicate multiple-word sequences, like "Grocery" can potentially lead to "Grocery Store", and Grocery Store" can lead to "Grocery Store Items"). The multi-word sequences read from the file can be tested against recognizable sequences, and thus your program can determine that it reached a header called "Grocery Store Items" (which it recognizes).

At this point, it can call a function that reads and handles the part of the file that represents Grocery Store Items. In which case a newline character represents that a new item is to be described, and then the first word is the item name, the second is the purchase value, the last is the sell value. Knowing the format, you can now read in and store only the parts of the file that interest you (e.g. parse, then store only the name portions, or the purchase value portions, etc).

The above is fairly rough, I'm just describing the general idea behind how to parse a file to get the data you want from it.

Also, -- structuring your file syntactically in a way that is concise, descriptive, yet short can greatly ease the parsing process. E.g. my above discussion took your example file without suggesting any improvements, and started trying to determine multi-word-sequences conditionally. But if your file instead used a special character or a small set of characters to define headers, or used single-word keywords (no sequences) etc, the parsing could be simplified to looking only for that character, or set of characters.

As an example approach of structuring data in text files, you may find XML interesting.

I hope this hasty post is somewhat helpful to you,

Ogoyant
Last edited on
Thanks to both you! These were both very helpful responses! :) Ogoyant il be looking into that file parser so ive changed the name of the stores to:

Ex
( ) store items
(item) (purchase value)/(sell value)

Food
Fish 5 7
Vegetables 10 15
Drinks 20 30

Guns
Pistol 300 375
Rifle 400 500
Ammunition 20 30









Last edited on
Good to know it helped. Yes, that would be easier to parse. You could implement multi-word sequences using underscores, like "Grocery_Store_Items".

Also, if you structured your headers with a specific character/s, like : <Grocery_Store_Items>, you could implement searches through the file where you would not have to compare each word you come across, but parse until you find a "<" , which indicates the start of a header name, then parse the name until the ">", and then check to see if it is the header file you are interested in. If not, just repeat the process of looking for the next header, designated by the next "<".

It may also be a good idea to designate not only the start of a section (like you do with "Grocery_Store_Items"), but also the end of it. This would help in reaching the end of the section without unnecessary word comparisons.

I'm again using an XML example because it is easy and descriptive: but if you had:

<Grocery_Store_Items>

...

</Grocery_Store_Items>

you could easily know the difference between "Drinks" and "Weapon_Store_Items", without too many conditional comparisons. Once you reached ">" of "<Grocery_Store_Items>", you'd know that you are at the start of the grocery store items section, which you are not interested in currently reading any content from. At this point you could store this string as a currentElement string and then, in order to skip the section, you could simply look for the next "<" in the file, ascertain that it is followed by a "/" (indicating the end of a section of your file), and then reading the name and comparing it with the currentElement, you'd know that the currentElement ("Grocery_Store_Items") reached it's end. Now, you could start parsing, repeating the process of looking for "<", until you found the section whose header name corresponds to what you are looking for (e.g. <Weapon_Store_Items>, <Guns>, etc.)

The above is just an example using XML element tags " < > " aimed to convey the general idea, you can structure your file any way that is most convenient for the data you want to store and represent.
Last edited on
Topic archived. No new replies allowed.