简体   繁体   English

在C ++中运行cmd命令

[英]Running cmd commands in C++

I am trying to make a text adventure based on C++. 我正在尝试基于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++. 我在这里遇到问题,因为我不知道如何在C ++中更改像“cd。\\ south”这样的目录。 Please tell me how to change directory in C++. 请告诉我如何在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. 我还搜索了谷歌,但它提供了另一个名为“ShellExecute”的函数的链接,我不知道如何使用它。 Please help (I am a complete beginner). 请帮忙(我是一个完整的初学者)。

The system function create a new process for the command. system函数为命令创建新进程。 This means that any directory changing will be local to that new process. 这意味着任何目录更改都将是该新进程的本地目录。

You want the _chdir function instead: 你想要_chdir函数:

_chdir("south");

Alternatively you can use the WIN32 function SetCurrentDirectory . 或者,您可以使用WIN32函数SetCurrentDirectory

Note: _chdir is the Windows CRT function name, on POSIX systems (like Linux or OSX) it's chdir (without the leading underscore). 注意: _chdir是Windows CRT函数名,在POSIX系统(如Linux或OSX)上它是chdir (没有前导下划线)。

Direction 1: Simply What you have to do is change the current directory . 方向1:简单您需要做的是更改current directory for this read this article http://msdn.microsoft.com/en-us/library/windows/desktop/aa363806(v=vs.85).aspx 为此阅读本文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 . 因为当前目录对于整个应用程序是通用的,所以其他线程可能会更改应用程序current directory

Direction 2: If you need to do this by executing system command (I don't know weather it is possible). 方向2:如果你需要通过执行系统命令来做到这一点(我不知道天气是可能的)。 then you can execute multiple system command using && in windows environment. 然后你可以在windows环境下使用&&执行多个系统命令。

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

The problem is that each system command will be executed in separated processes, so your cd command will work but won't be effective for the next commands. 问题是每个system命令都将在单独的进程中执行,因此cd命令将起作用,但对下一个命令无效。

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. 你可以使用chdir如果你在Linux / Unix系统或SetCurrentDirectory for Win32 API,但我不确定它是否真的是你想要做的。

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 你应该至少有以下类: LocationObjectCharacter

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 . 每个位置应具有:名称,描述和公共出口的十个位置的向量,例如西nenwsesw向上向下 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. 您可以使用现有的系统,例如Inform,但我不知道您是否正在尝试锻炼您的C ++技能。

Remember you can look for the help of true experts in adventures by visiting the interactive fiction forum: 请记住,您可以通过访问互动小说论坛寻找真正的冒险专家的帮助:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM