Navigating with File I/O
There seems to be a great lack of online information on file i/o, and the one good resource I found still didn't answer the question I was looking for.
I am looking into this as part of a self-created project. I earlier wrote a text-based game which passes through areas by returning function pointers to hard-coded 'maps'. I was hoping to migrate all of the maps into text or binary files as practice, but what I had imagined as the solution isn't possible based on the resources I've found. I'm wondering if this is possible at all.
What I was hoping was to have the maps formatted like this:
but I haven't found any information on how to navigate around input files, that is, how to use "//Area1" as a delimiter, or how to take the information from the "//Movement" section and use that to search through the file for the next area.
Level description etc etc
Opt 1: move to area1b
Opt 2: move to area2
Anybody have any links or resources on this topic, or know that it simply can't be done?
There are several ways to navigate through files.
On way is to have each of your sections a fixed size. Then knowing the size of the block just skip to the next block. This usually requires padding each section with a fixed number of characters to insure the sizes are consistent.
Another way is to search each section identifier and record it's location (tellg()) in an index file. Then it is just a matter of creating a map and then loading in this information. The map would have the search string "//Area1" and the value returned from the tellg() in your initial search.
Just hit another snag. I should say that I'm making this as an engine. There is a 'main game file' that contains the addresses on disk of all the subfiles, such as the maps. But when I try to use getline() to take one of the addresses and store it in a <string>, the compiler doesn't like that. When I typecast the string as (char*), it compiles, but is never able to load the file. When I type the address in manually (ie, hardcode it), it loads just fine.
What is the most standard way to load addresses (ie: /home/user/myfile.txt) from within files?
Please show a sample of the main game file. You should be able to store the file name in a string then use string.c_str() to open the file.