Thread: Navigating with File I/O

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    31

    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:

    Code:
    //Area1
        Level description etc etc
    //Options
        Opt1 desc
        opt2 desc
    //Movement
        Opt 1: move to area1b
        Opt 2: move to area2
    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.

    Anybody have any links or resources on this topic, or know that it simply can't be done?

    Thanks!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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.

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    Thanks, Jim.

    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?

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You can use the Boost program_options library's parse_config_file function.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    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.


    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM