Is there any alternative for clrscr() and gotoxy in gcc ? I am using system("cls"); to clear screen but it doesn't identify cls as a valid command whereas in cmd cls is working perfectly on windows? can anyone please help ? i am using gcc as my compiler and using dev cpp and codeblocks as my IDEs. thanks in advance.

On linux systems, the shell command to clear the screen is "clear", not "cls". Sometimes a form-feed output will do this as well.

@rubberman Can you elaborate it more ? I have explained more about my problem. Please explain little bit more. Am using DEV CPP and writin code in C. now can you help me more ? thanks in advance sir.

Use the PDCurses library for all the console control you could ever want.

Step back a moment and consider why you need clrscr or any variations. It's my fairly strong belief that when you start needing to do any non-linear manipulation of the console, you should probably bite the bullet and move to a GUI.

That said, if you're on a Windows system you can write a clrscr (all of the conio library, in fact) that's portable to any Win32 compiler: https://code.google.com/p/c-standard-library/source/browse/src/internal/_sysconio.c. The relevant parts are:

#include <Windows.h>

void clrscr(void)
{
    HANDLE sys_stdout = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO info;
    COORD topleft = { 0 };
    DWORD size, written;

    if (!GetConsoleScreenBufferInfo(sys_stdout, &info))
        return;

    size = info.dwSize.X * info.dwSize.Y;

    /* Overwrite the visible buffer with blank spaces */
    FillConsoleOutputCharacter(sys_stdout, ' ', size, topleft, &written);

    /* If the first call succeeded, this one should too */
    GetConsoleScreenBufferInfo(sys_stdout, &info);

    /*
        Fix the character attributes (color, etc...) for the "whitespace"
        if they weren't set to defaults. Otherwise they would be lost.
        Finally, reset the cursor position.
    */
    FillConsoleOutputAttribute(sys_stdout, info.wAttributes, size, topleft, &written);
    SetConsoleCursorPosition(sys_stdout, topleft);
}

Using the OS command for clearing the screen is bad for security reasons. For example, someone could map cls (or clear) to another program, and yours would run it without realizing.

If you are worried about what's on the screen for others to see, just use a for loop printing a bunch of empty lines.

If you are worried about what's on the screen for others to see, just use a for loop printing a bunch of empty lines.

I'd wager that 10 times out of 10, when someone wants to legitimately use clrscr it's to repaint a console based menu or faux graphical layout. Which is why I suggest that such usage is unnecessary and time would be better spent with a proper GUI library.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.