-1

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).

pranphy
  • 1,787
  • 4
  • 16
  • 22
  • 1
    If you want your code to be portable, you can't call arbitrary system commands that may or may not exist on any particular system. In fact, you shouldn't assume the system has a screen until you check if it does. What would "CLS" do if you're talking to a line printer or text-to-speech engine? What would it do if your output was redirected to a file? – David Schwartz Nov 14 '12 at 17:40

3 Answers3

16

The problem is that the command CLS does only exist on Windows. For Linux, the command you want is: 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.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • 1
    Wow.. Call it the Mandela effect but I was sure years before I used `cls` // `clr` I always had it in my head dos etc was more verbose .. Even got me here to realise otherwise. Thanks? – Pogrindis Nov 04 '21 at 01:52
4

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

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

larsks
  • 277,717
  • 41
  • 399
  • 399
0

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 clear or use ANSI escape codes to do the work for you. Neither are portable, but both are seen in wide use.

Community
  • 1
  • 1
user7116
  • 63,008
  • 17
  • 141
  • 172