Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何使Ubuntu命令在Windows上工作_C_Windows_Ubuntu_Operating System_Command - Fatal编程技术网

C 如何使Ubuntu命令在Windows上工作

C 如何使Ubuntu命令在Windows上工作,c,windows,ubuntu,operating-system,command,C,Windows,Ubuntu,Operating System,Command,我在Ubuntu上运行的C代码有一行 system("ls -l | wc > temp.txt"); 我想让它在windows上工作,这样它就必须是独立于操作系统的。我该怎么做呢。 有人能帮我吗?我猜显示的特定代码可能会在某个时候从“temp.txt”文件中获取第一个值,并将其用作文件计数(实际上是文件数加一) 相反,您可以使用这样的C代码 #include <stdio.h> #include <sys/types.h> #include <dirent

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

system("ls -l | wc > temp.txt");
我想让它在windows上工作,这样它就必须是独立于操作系统的。我该怎么做呢。
有人能帮我吗?

我猜显示的特定代码可能会在某个时候从“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);
}
#包括
#包括
#包括
int main(){
处长*cwd;
int c=1;/*like+1*/
结构方向*d;
如果((cwd=opendir(“.”)){
而((d=readdir(cwd))){
如果(*(d> > dyNeNd)!= ''))C++,/*忽略点文件*/
}
}否则{
perror(“opendir失败”);
申报表(1);
}
printf(“temp.txt中的第一个数字是%d”,c);
返回(0);
}

无论system()调用的结果是什么,我的答案是:用C语言重写它,您可以在两个系统上使用C语言

如果您的代码需要跨不同的操作系统移植,那么很简单:不要使用
system()
。回答您的问题:您可以在模拟POSIX环境的Windows上安装一个层,如MinGW或Cygwin。@H2CO3 Windows中的POSIX子系统足以完成此任务。不需要其他库。@是否可检测?所以,如果我继续在cmd.exe中键入
ls-l | wc
,我会得到我的结果吗?我对此表示怀疑。存在大量的偏差和缺失(库、API、工具等),使Windows无法作为适当的POSIX系统使用,即使它包含POSIX子系统。@H2CO3您将POSIX子系统与命令行解释器混淆了。同样,Windows中的POSIX子系统已足够完成此任务。@IInspectable此外,
ls
wc
的可用性不依赖于命令行解释器。如果安装了它们,那么它们可以在任何命令解释器下运行。POSIX标准需要一系列命令行工具,它们的存在与特定系统上使用的特定命令行解释器无关。因此,如果Windows符合POSIX,就可以将
ls-al
敲入cmd.exe并得出结果。