C语言 Linux 控制台;未找到 CLS 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13384116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:25:39  来源:igfitidea点击:

Linux console ; CLS not found error

cconsolesystemubuntu-12.04

提问by pranphy

I am trying to compile a C project that I wrote while I used windows. I am trying to compile same project with same IDE (Code::Blocks) in Linux (Ubuntu 12.04). I have several System("CLS")functions used in my program. But the linux console says
sh: 1:CLS: not found
Segmentation fault (core dumped)

I have included <stdlib.h>and <stdio.h>(and several of course).

我正在尝试编译我在使用 Windows 时编写的 C 项目。我正在尝试在 Linux (Ubuntu 12.04) 中使用相同的 IDE ( Code::Blocks)编译相同的项目。System("CLS")我的程序中使用了几个函数。但是 linux 控制台说 我已经包括了和(当然还有几个)。
sh: 1:CLS: not found
Segmentation fault (core dumped)

<stdlib.h><stdio.h>

回答by Baltasarq

The problem is that the command CLS does only exist on Windows. For Linux, the command you want is: clear.

问题是命令 CLS 只存在于 Windows 上。对于 Linux,您需要的命令是:clear.

Anyway, that will only partially solve the problem: a) you'll have the same problem if you port your program to a windows machine, and b) there is no reason for that to produce a segmentation fault.

无论如何,这只能部分解决问题:a) 如果您将程序移植到 Windows 机器上,您将遇到同样的问题,并且 b) 没有理由产生分段错误。

回答by larsks

The systemcommand runs shell commands on your local system. clsis a valid DOS/Windows command, but it does not exist on Linux. The clearcommand on Linux provides similar functionality.

system命令在您的本地系统上运行 shell 命令。 cls是一个有效的 DOS/Windows 命令,但它在 Linux 上不存在。clearLinux 上的命令提供了类似的功能。

The segmentation fault suggests you are not correctly handling the error.

分段错误表明您没有正确处理错误。

回答by user7116

CLS is an old MS-DOS command not likely to be found on your Linux machine. You should not rely on spawning another process to clear your console text for you. You can call out to clearor use ANSI escape codesto do the work for you. Neither are portable, but both are seen in wide use.

CLS 是一个旧的 MS-DOS 命令,在您的 Linux 机器上不太可能找到。您不应该依赖生成另一个进程来为您清除控制台文本。您可以调用clear使用 ANSI 转义码来为您完成这项工作。两者都不是便携式的,但都被广泛使用。