0

what function I should use for clear screen in C on codeblocks?

I used system("CLS"); but it's not working on codeblocks.

9th Apr 2019, 6:37 AM
tahmid tabib
tahmid tabib - avatar
4 ответов
+ 2
'system' is a windows specific command that's slow and insecure. You could easily replace the file that handles it by something malicious, so you shouldn't use it anyway. That said the console is a 1d array somewhere in memory. Each cell exists out of 2 bytes, the 1st byte is reponsible for the character itself while the 2nd byte contains the attributes for the character. This is how coloring is made possible. This is not a windows specific thing, you'd get the exact same behaviour if you had access to the BIOS or direct access to video memory. But since these areas are in the < 1 Mb region of your RAM and most operating systems block this area from users ( for good reason ), the operating systems provide functions to the users to manipulate the screen anyway. How depends on the operating system. Linux uses ANSI escape code https://en.wikipedia.org/wiki/ANSI_escape_code You can easily clear the screen and move the cursor to the top left with the following: printf( "%s", "\033[2J\033[1;1H" ); This works for me at least. Windows however does not support ANSI and it gets a bit longer there. First you need to get a handle to the console window then ask windows for the console-screen-buffer info and then call 1 function that sets the upper byte and another function that sets the lower byte. The upper byte is set to a bunch of spaces, though null characters also seem to work just fine. The lower byte is set to a value of 7, which means no coloring. Put everything together and you end up with this code: https://code.sololearn.com/ck9435XF9iYU/?ref=app
9th Apr 2019, 8:32 AM
Dennis
Dennis - avatar
+ 2
~ swim ~ Oh, you're right. Well, fact remains that you should avoid it as it is a huge security hole. At least on windows. Probably on android too if it stores the string in plain text. It is not portable either, not all OS's support the same command, cls clearly being the exception in your case.
9th Apr 2019, 9:34 AM
Dennis
Dennis - avatar