reading from a file depending on variable

This is a discussion on reading from a file depending on variable within the C++ Programming forums, part of the General Programming Boards category; well I have been messing around with file input and output as well as trying to fix up my old ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    17

    reading from a file depending on variable

    well I have been messing around with file input and output as well as trying to fix up my old text based adventure game. At the moment everythink is going well and I already have planned how I will do all my features. The issue is I have hit a problem which I am not sure how to get around with out using a cheap trick.

    My idea is to load all map data from files. This will make my code alot less messy as well as meaning I won't need to hard code stuff. I have worked out how to do everythink but I am once again in need of your help. This is how I plan to do it.

    1.reads from a txt the map description e.t.c
    2.stores everything in variables.
    3.prints the map description.
    4.player can choose if he wants to move n,s,e,w
    5.if the player for instance picks north it will look at the north variable (lets say its 2) and then reload all the variables from the text file called number 2.

    the issue is I am having problems getting the computer to read from a file depending on the vairable name I have tried many combinations my latest has been.

    ifstream readMap("C:\\Dragonways\\Map\\" << (mapfile[0]) << ".txt");

    but I have not had any luck. Any help would be really nice.

    thanks for reading
    zidsal

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    This will only work with cout and family.
    "My str" is a string literal, a pointer, const char*, so this approach would attempt to shift the pointer left X steps, and not append data.
    Instead, you must create a proper string first and then pass it to the ifstream constructor.
    To assist in this, you might want to use string streams. Essentially create a stream, then use the overloaded operator << to put data inside it and the pass the resulting string to the constructor.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    Quote Originally Posted by Elysia View Post
    This will only work with cout and family.
    "My str" is a string literal, a pointer, const char*, so this approach would attempt to shift the pointer left X steps, and not append data.
    Instead, you must create a proper string first and then pass it to the ifstream constructor.
    To assist in this, you might want to use string streams. Essentially create a stream, then use the overloaded operator << to put data inside it and the pass the resulting string to the constructor.
    from what I managed to understand from this, it may be problamatic later on in the program. The reason I wnated it as a char array instead of a string is so I could convert it to a integer later on. That is unless I have missed something big with the function atoi.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    You can convert a string to an int with atoi (just call c_str() on the string to get a character array). I would use stringstreams instead of atoi, though.

    If mapfile[0] is a string then you can use + to combine it with the other parts of the path:
    Code:
    string filename = "C:\\Dragonways\\Map\\";
    filename += mapfile[0];
    filename += ".txt";
    
    ifstream readMap(filename.c_str());
    Otherwise you'll want to use a stringstream for that as well.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Of course, I don't think std::string will accept any integer input, but if it's a string, then just adding them together like Daved shows will work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    well I have now got an error I have never seen before in dev c++. When I hit compile it says out of memory allocating 65536 bytes. It dosn't highlight any lines. Once this error shows up windows then says its out of virtual memory. Any help?

    edit
    fixed made the atring array 1 diget too big.
    Last edited by zidsal; 06-12-2008 at 01:24 PM. Reason: solved

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Try a recompile?
    Failing that, try another compiler and see if you find any compile errors. If so, fix them and try recompiling with Dev-C++ again.
    If it still doesn't work, which is unlikely, then it might just be a compiler bug O_O
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    o.k I kept gettin g the same error, sometimes it would produce compile errors sometimes it would say it was out of memory. The sollution is a reboot and now it works like a charm. Thanks guys.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Corrupted OS state, then. That would explain it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Not really addressing your question, but it makes me think.

    I kind of like the idea of loading room descriptions on-demand from external files. Of course, the world should definitely be described externally, not coded into the program, but the question is, do you load the world once at the beginning of the program, or do you load each room description when that room is accessed?

    Leaving aside the question of efficiency, I like the idea of loading the rooms on-demand because it means you can update the world while the game is running just by editing a file.

    Now, you should make it all "enterprisey" and create a DTD to describe your rooms using XML and load them into the program as a DOM, with the whole thing being scriptable with XSLT and JavaScript

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    too make life easyer for me I just load the details of the room thats needed. If the player requests to move I then clear the details of the variables and reloads the new details into place.

    if there is one think I ever lot from my 2d vb6 engine days it is don't load everythink at once. Furthermore by making it load one thing at once the only map code you will ever need is the reloading, the rest you can store in files. My files are set out like this.

    room description
    north number
    south number
    east number
    west number.

    if the player requests to move north I just get the program to read the north number, clear everything and load the map that is the north number.

    thinking about it I will 100&#37; do this with monsters. But I am not so sure about items as they will all have different uses. I may just load the basics from notepad and have another function that cheaks what item they are. If its a certain number the computer will operate the hardcoded section as well.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    sorry to double post. I did cheak the forum rules and they never said it isn't allowed but I am sure its going to be one of those un written rules like necroing.

    I know it seams I am getting everyone to do everythink for me but thats because my only resource at the moment is the internet and cprogramming. If I can get this small project running I will proberly invest some money in soem books.

    the point is I am now having issues with setting the variable to null and reading the file. Yes I am such a pain.

    Code:
    int mapReload()
    {
     filename = "C:\\Dragonways\\Map\\" + mapfile + ".txt";
     /* reset all map variables to blank */
    
    mapdescription = "NULL";
    north = "null";
    south = "null";
    east = "null";
    west = "null";
    
     ifstream readMap(filename.c_str());
     getline(readMap,mapdescription);
     getline(readMap, north);
     getline(readMap, south);
     getline(readMap, east);
     getline(readMap, west);
    
    
     cout << mapdescription; 
     cout << "\ndebugging"; 
    }
    The first thing is the compiler things the null value is a string of letters called n-u-l-l and not acterly meaning nothing. Second of all it would acterly put the first line into the variable map description. Everything else is printing out fine so its 100% a reading issue.

    thanks
    zidsal

    p.s anyone know any nice c++ books to buy>

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Double posting is generally not well liked is many forums unless you have a good reason to do so. I'd classify this one as a good one.
    There's no null in C++ and I don't think there will ever be. "null" is a string, not literally null, so that's why it won't work.
    But you can just set the string to "" (empty) and then check for empty if you want. But you can just replace the data inside the string with new data.

    I'm not very familiar with any newbie C++ books, but I do have about two good advanced books. Anyway, the good books sticky should help you find some good books.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by zidsal View Post
    sorry to double post. I did cheak the forum rules and they never said it isn't allowed but I am sure its going to be one of those un written rules like necroing.
    By "double post" do you mean posting on a different topic in the same thread? It's not against the rules, it just hurts you because people might have already become bored with this thread. If you want people to treat your question as a separate topic (which it is), just post it in a separate topic. We certainly don't mind.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    17
    well my mapping system is around 80% complete done because the basics are now complete BUT

    one bug thats been developed. Its a really annoying bug but its nothing major.


    Code:
    filename = "C:\\Dragonways\\Map\\" + mapfile + ".txt";
    ifstream readMap(filename.c_str());
    the following code will make it so it will not read the file (mapfile variable).txt. But if I write the following code.

    Code:
     filename = "C:\\Dragonways\\Map\\" + mapfile + "0.txt";
    ifstream readMap(filename.c_str());
    the following code will make it so it reads the file (mapfile variable)0.txt.

    its nothing major but I would really like it fixed.

    thanks
    zidsal

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Reading from file into structs..
    By dankas in forum C Programming
    Replies: 14
    Last Post: 10-16-2002, 10:33 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21