Jump to content
  • Advertisement
Sign in to follow this  
mikenovemberoscar

File input/output in C++ Question

This topic is 1996 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

 

I am making a game in Visual C++ 2010, and I want to have my levels stored as text files generated by my level editor.

 

I know how to make it so the compiled finished executables can read files from a fixed path (C:/etc/etc), and a rough idea of how to make it read files relative to the executable itself.

 

But how do I make it so I can include a level file (in my Visual C++ solution) which is read by the program at runtime (using file streams) - but will be packaged with the executable when compiled and finished and invisible to the users?

 

 

Thanks in advance,

 

mikenovemberoscar

Share this post


Link to post
Share on other sites
Advertisement
I am not sure what functions you are using for file access, but they typically specify the name of the file as a `char const *'. You should probably use `std::string' for most things, and when it is time to call the function that expects a `char const *' you can use `my_string.c_str()'.

Share this post


Link to post
Share on other sites

The program will look for the file in the executable's directory.

You can't really make your level files invisible, but you can hide them by packing all your level files in a "level" folder and placing the folder in your executable's directory.

Then you can load the levels like

 

loadLevel("level/level1.txt");

loadLevel("level/level2.txt");

Edited by lride

Share this post


Link to post
Share on other sites

If you aren't using files, you don't need to use file streams. If you are bundling your data into the executable, it is at some level merely memory that you are reading. I believe there are APIs for reading resources in Windows for example, but I don't have any experience with them so I will say no more.

 

One option is to use something like physfs and bundle all the resources for your game into a second file. So you end up with two files, your "game.exe" and your "game.data" (or whatever you want to call it). The data file is actually some kind of ZIP archive containing the same directory layout you use during development.

Share this post


Link to post
Share on other sites
Okay thanks all,

If you are bundling your data into the executable, it is at some level merely memory that you are reading
Yeah this one.

I can include header files and use them in my game, but they aren't invisible to the end user?
Can I do the same thing with level files? I like the idea of resource files (.rez if i remember correctly?) but most professional games nowadays can be run with just the executable, or am I missing something?

Sorry I'm kind of new to Windows. Edited by mikenovemberoscar

Share this post


Link to post
Share on other sites

[quote name='mikenovemberoscar' timestamp='1357214576' post='5017062']
Can I do the same thing with level files? I like the idea of resource files (.rez if i remember correctly?) but most professional games nowadays can be run with just the executable, or am I missing something?
[/quote]

Resources are embedded inside the executable, so I suppose yes. That said, I don't know many AAA games that can be run with just an executable, usually they have a data folder containing tons of data. If the game packed everything inside a single .exe, that file would be absurdly large and it would make patching rather difficult.

Share this post


Link to post
Share on other sites
Ah I see... I examined my games and I realise they're all shortcuts. But can't resource files be read and modified by the user?

 

Yes, they can, just like everything else on their computer. There is no good rationale to actively prevent the user from modifying data files, at least in a single player game. If the player wants to mess with the data files (perhaps to edit textures or change stats) you should let him. It's not like it's harming anyone, and it adds replay value to the game in the form of unofficial modding.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!