File input/output in C++ Question

Started by
12 comments, last by Bacterius 11 years, 3 months ago

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

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()'.

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");

An invisible text.

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.

If you go the Windows resources route, the relevant loading functions are FindResource(), LoadResource() and LockResource(). In many cases you'll also want to use SizeofResource().

That said, I prefer using a renamed zip file with PhysFS.
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.

[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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

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?

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.

“If I understand the standard right it is legal and safe to do this but the resulting value could be anything.”

Well I see your point, but what if my game was multiplayer? Just write the files in a binary format and hope for the best?

This topic is closed to new replies.

Advertisement