UtkarshSahu
UtkarshSahu

Reputation: 93

Running cmd commands in C++

I am trying to make a text adventure based on C++. I have made folders which contains the specific files to each path. Suppose I go south from a room, i need to go into a folder named "south". I am having problems here as I don't know how to change directory like "cd .\south" in C++. Please tell me how to change directory in C++.

I tried to use:

system("cd .\\south")

but it does not change directory. I also searched on Google but it gives link to another function called "ShellExecute" which I don't know how to use. Please help (I am a complete beginner).

Upvotes: 1

Views: 4411

Answers (4)

Baltasarq
Baltasarq

Reputation: 12242

The answer to the specific question you are doing have already being given. However, I wonder why you are trying to change the current directory in order to answer to a command given by the user.

Maybe you are doing it this way because you want to gain knwoledge something specific, however, take into account that the average way to face a text adventure is not to create folders in the computer, but to create the appropriate structures.

You should have classes at least for: Location, Object, Character

You should have vector of locations and objects in order to represent all possible locations and objects in the game.

The playing character should also have a list of objects that he is able to carry with him (thogh you can make that extensible to other characters in the game).

Each location should have a: name, description, and a vector of ten positions for the common exits, such as north, south, east, west, ne, nw, se, sw, up and down. Inside that vector, you could store the number of the Location to go when that exit is chosen.

And finally, you need to parse the input of the player, in order for the game to be able to understand the commands.

This of course, are the minimums of the adventure. You can use already existing systems, such as Inform, though again I don't know if you are trying to exercise your C++ skills.

Remember you can look for the help of true experts in adventures by visiting the interactive fiction forum:

http://www.intfiction.org/forum/

Upvotes: 0

Nayana Adassuriya
Nayana Adassuriya

Reputation: 24766

Direction 1: Simply What you have to do is change the current directory. for this read this article http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806(v=vs.85).aspx

But if your application is multi threaded. then you need to be careful. because current directory is common for the whole application so other thread may change the applications current directory.

Direction 2: If you need to do this by executing system command (I don't know weather it is possible). then you can execute multiple system command using && in windows environment.

EG: system("cls && date && pause");

Upvotes: 2

zakinster
zakinster

Reputation: 10698

The problem is that each system command will be executed in separated processes, so your cdcommand will work but won't be effective for the next commands.

You could use chdir if you're on a Linux/Unix system or SetCurrentDirectory for Win32 API but i'm not sure whether it's actually what you want to do.

Upvotes: 0

Some programmer dude
Some programmer dude

Reputation: 409482

The system function create a new process for the command. This means that any directory changing will be local to that new process.

You want the _chdir function instead:

_chdir("south");

Alternatively you can use the WIN32 function SetCurrentDirectory.

Note: _chdir is the Windows CRT function name, on POSIX systems (like Linux or OSX) it's chdir (without the leading underscore).

Upvotes: 2

Related Questions