SOLVED: C++ Reading both strings and integers from a .txt file

Started by
6 comments, last by rpg_code_master 18 years, 8 months ago
When my game sets up a new level, it loads in a text file, named 001.txt for the first level, and then reads in commands like those below. For example: [SPRITES_LOAD] data/sprites1.bmp This instructs the program to load a bitmap containing sprites. [SPRITES_COPY_FLIP_HORZ] 1 8 -1 This instructs the program to create two new sprites by copying sprites 1 and 8 and flipping them horizontally. It reads the integers until it reaches -1, which tells it to stop reading integers. This is a very simple system and all works perfectly well. What I want to do though is to be able to define phrases within the text file, so that I could put something like this: [DEFINE] FIRSTMONSTER 8 ...near the start of the text file. Then I could put: [SPRITES_COPY_FLIP_HORZ] 1 FIRSTMONSTER -1 The [DEFINE] command would tell the program to search through the file and replace any occurance of the phrase 'FIRSTMONSTER' with '8'. At present my program works directly from the text file, but I need to do things differently if I'm going to add this [DEFINE] command. I'm thinking that I need to first of all copy the entire text file into a string object, then have a routine that works through the string looking for the command [DEFINE]. When it finds this command it can then look ahead through the string object and do the search and replace, eg replacing 'FIRSTMONSTER' with '8'. I think I know how to do this, but it seems a bit clumsy. Can anyone point out any pitfalls or suggest a better approach? It needs to be simple! [Edited by - darenking on August 2, 2005 4:35:10 AM]
Advertisement
Changing the entire text for every token would be pretty messy.

Here's what I would do:

Keep a vector of tokens. Each entry has the token's name ("FIRSTMONSTER") and the token's value (8). Every time you hit a [DEFINE] command, add it to your tokens.

Go through and parse the file as you were before.

Add some code to the part where you parse numbers. If the number-parser fails to parse a number (for example, if it tries to parse FIRSTMONSTER as a number), then go look through your list of tokens. If you find the text in your list of tokens, then use the token's value as the value. If you don't find it, then crap out.

Also, it's 2005! Use XML =D
What's the advantage of XML? I don't know anything about it really.

What would the tokens look like? Two vectors, one of strings and one of ints?

Also do you agree that I should copy the entire text into a string object?
you should probably use a binary file format unless you want users to be able to easily edit the data themselves. if you dont know how to do binary file access then now would be a good time to learn. if you are programming for a Windows OS then check out:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/file_management_functions.asp

i would suggest writing a File class that encapulates some of those functions including:

CreateFileW
ReadFile
WriteFile
GetFileSizeEx
SetFilePointerEx
SetEndOfFile
CloseHandle
You have to read each token as a string, then look it up in your definition table to see if you need to do a substitution before actually passing it back to whatever code uses the file. Writing such a 'read' function should be simple enough - there is no need to read the entire file and do substitutions before hand.

Hmmmmm, the new Boost iostream framework would be ideal for that kind of things...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by darenking
What's the advantage of XML? I don't know anything about it really.

What would the tokens look like? Two vectors, one of strings and one of ints?

Also do you agree that I should copy the entire text into a string object?


The advantage of XML is that there are lots of XML readers that already exist, so you don't have to worry about menial text processing. I don't know how your current processor works, but I'm guessing it's something like: 1) read a line, 2) find the [, 3) read everything until you find a ], 4) find out what command was in between the [ and the ], 5) do something with the remaining text. The XML parser would take care of that stuff for you. It leads to much simpler & cleaner code. Also XML is a text-file format, so you still have the benefit of editing it by hand.

Storing the tokens with two parallel vectors is fine. You could also make a Token struct (that has a string & an int), and keep a vector of Tokens.

Copying the entire text to a string is fine, if that makes things easier.
Is there a library or something that reads the XML?
Try TinyXML. It’s really simple to use.

This topic is closed to new replies.

Advertisement