【问题标题】:How to make Ubuntu commands make work on Windows如何让 Ubuntu 命令在 Windows 上运行
【发布时间】:2014-01-16 13:03:46
【问题描述】:

我在 Ubuntu 上运行的 C 代码有一行

system("ls -l | wc > temp.txt");

我想让它在 Windows 上运行,这样它就必须独立于操作系统。我该怎么做。 谁能帮帮我?

【问题讨论】:

  • 如果您的代码需要跨不同操作系统移植,那么很简单:不要使用system()。要真正回答您的问题:您可以在模拟 POSIX 环境的 Windows 之上安装诸如 MinGW 或 Cygwin 之类的层。
  • @H2CO3 Windows 中的 POSIX 子系统足以完成此任务。不需要额外的库。
  • @IInspectable 是吗?所以如果我继续在 cmd.exe 中输入ls -l | wc,我会得到我的结果吗?我不信。有大量的偏差和遗漏的东西(库和 API、工具等)使得无法将 Windows 用作正确的 POSIX 系统,即使它包含 POSIX 子系统。
  • @H2CO3 您将 POSIX 子系统与命令行解释器混淆了。同样,Windows 中的 POSIX 子系统足以完成这项任务。
  • @IInspectable 此外,lswc 的可用性确实 NOT 取决于命令行解释器。如果安装了它们,那么它们可以在任何命令解释器下运行。有一个 POSIX 标准所需的命令行工具列表,它们的存在与特定系统上使用的特定命令行解释器独立。因此,如果 Windows 符合 POSIX,则可以将ls -al 敲入 cmd.exe 并输出结果。

标签: c windows ubuntu operating-system command


【解决方案1】:

我猜想显示的特定代码可能会在某个时候从“temp.txt”文件中获取第一个值,并将其用作文件数(实际上是文件数加一)

你可以使用这样的 C 代码来代替它

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>

int main() {
DIR *cwd;
int c=1; /* like +1 */
struct dirent *d;
if ((cwd=opendir(".")) ) {
 while((d=readdir(cwd))) {
     if (*(d->d_name) != '.') c++; /* ignore dot files */
 }
} else {
    perror("opendir fail");
    return(1);
}
printf("the first number in temp.txt would be %d", c);
return(0);
}

无论 system() 调用结果在做什么,这就是我的答案:用 C 重写它,你在两个系统上都可以使用它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2016-10-16
    • 2018-09-21
    • 2013-09-13
    • 2018-04-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多