我无法使用 C++ 进入目录

发布于 2024-10-09 15:20:15 字数 237 浏览 0 评论 0原文

我需要更改项目的工作目录,以便输出文件转到某个文件夹,而不是所有项目文件所在的位置。

我正在使用

system("cd secretdir/");
system("ls");

但是,我得到的是当前项目目录中的文件列表,而不是“secretdir”文件列表。

我使用的是 Mac OS X 10.6/Qt Creator 4.7 64 位

谢谢!

I need to change working directory of my project, so that output files go to a certain folder, not where all the project files are.

I'm using

system("cd secretdir/");
system("ls");

However, what I get, is the list of files in current project directory, not the "secretdir" one.

I'm on Mac OS X 10.6/Qt Creator 4.7 64 bit

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

人│生佛魔见 2024-10-16 15:20:15

您必须更改当前工作目录

http://www.linuxquestions.org/questions/programming-9/how-to-change-current-working-directory-in-c-550031/

另外,您应该考虑保存输出具有完整路径名的文件而不是更改工作目录。

You have to change the current working directory

http://www.linuxquestions.org/questions/programming-9/how-to-change-current-working-directory-in-c-550031/

Also, you should consider saving your output files with full path names instead of changing the working directory.

倥絔 2024-10-16 15:20:15

您当前的代码将生成一个子 shell,该子 shell 会将其当前目录更改为 ./secretdir,然后继续执行 exit() 而不执行任何其他操作。

只有这样,ls 才会在另一个 子 shell 中运行,当然,该子 shell 的当前目录完全独立于您之前调用 system().

这可能就是你的问题所在。您是否在寻找 chdir() 函数?

chdir("secretdir");
// From now on, the current directory of the process is `./secretdir`.
system("ls");  // Will probably behave as expected.

Your current code will spawn a subshell that will change its current directory to ./secretdir, then proceed to exit() without doing anything else.

Only then will ls run in another subshell, whose current directory is, of course, completely independent of what you did during your previous call to system().

That's probably where your problem lies. Are you looking for the chdir() function?

chdir("secretdir");
// From now on, the current directory of the process is `./secretdir`.
system("ls");  // Will probably behave as expected.
岁月流歌 2024-10-16 15:20:15

编辑请参阅法尔马里的回应,因为我掩盖了您问题的第一句话。

您还可以使用 chdir

下面是很糟糕的

第一个系统产生了一个执行cd的新进程。第二个系统产生一个完全不同的进程,它不知道之前发生了什么。

您可以做的一件事是:

system("ls Secretdir/");

edit See Falmarri's response as I glossed over the first sentence of your question.

You can also use chdir

the following is crufty

The first system spawns a new process that does the cd. The second system spawns a completely different process that doesn't know what happened previously.

One thing you could do is:

system("ls secretdir/");

梦魇绽荼蘼 2024-10-16 15:20:15

我强烈建议您查看 QT Creator 帮助或在线文档中的 QDirQFileQProcess 对象,因为您正在使用它。他们有非常详细且易于理解的文档,并且使用 QT 中提供的工具应该是选择该工具的主要原因,在我有限的经验中,许多 QT 竞争对手都提高了可移植性和可用性。

另外,QTForum 上有一个很棒的 QT 相关问题社区,值得添加书签,特别是如果您是 QT Creator主要开发环境。

根据一般经验法则,应避免使用 system,它在许多情况下效率低下且不安全。

编辑:抱歉,我也掩盖了你的第一句话并跳到了代码位。您可以通过 QT Creator 中的“项目”选项卡修改项目设置,以将“自定义流程”步骤添加到构建中,您可以在其中指定工作目录,然后执行复制命令到您想要输出的任何位置。您还可以直接在 .pro 文件中指定构建输出选项...但是,帮助和文档再次成为您的朋友。

替代文字

I'd highly recommend checking out QDir, QFile, and QProcess objects in the QT Creator help or online documentation since you are using it. They have very detailed and easy to understand documentation and using the tools available to you in QT should be a primary reason for choosing that tool much of QT rivals boost in portability and usability in my limited experience.

Also there is a great community for QT related questions at QTForum worth bookmarking especially if QT Creator is your primary development environment.

Using system should be avoided as general rule of thumb it is inefficient and insecure in many cases.

EDIT: Sorry I too glossed over your first sentence and jumped to the code bits. You can modify the project settings via the Projects tab in QT Creator to add a Custom Process step to the build where you can specify a working directory and then do a copy command to wherever you would like your output to go. You also may be able to specify a build output option within your .pro file directly ... once again the help and documentation is your friend however.

alt text

烦人精 2024-10-16 15:20:15

Mac OSX 上的函数是 chdir("./secretdir"),尽管由于它是一个 POSIX API,所以实际上在许多其他平台上也能以相同的方式工作。

The function on Mac OSX is chdir("./secretdir"), although since it's a POSIX API it actually works the same on many other platforms as well.

云雾 2024-10-16 15:20:15

使用 system() 是不可移植的,所以尽量避免像这样直接使用“cd”。我的建议是使用 Boost 文件系统

有一个两分钟教程

Using system() is not portable so try to avoid to use directly "cd" like that. My advice is to use Boost filesystem.

There is a Two-minutes Tutorial !

逆流 2024-10-16 15:20:15

最好

system("cd secretdir/; ls");

还是使用 boost 的 文件系统 库。也许只是 opendir

Do

system("cd secretdir/; ls");

Or better yet use boost's filesystem library. Maybe just opendir.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文