Thread: Options for Clearing Output

  1. #1
    Registered User
    Join Date
    Apr 2016
    Location
    London, UK
    Posts
    3

    Question Options for Clearing Output

    I've been exploring certain ways of clearing output produced by C programs, so far I know of the '\b' character used within the program, and the system() function used to invoke the "clear" command in the terminal, which clears not only the output produced by the program, but any output in the terminal. I'm wondering if there's any more options that carry out more specific clearances/edits of current output, such as clearing only the output of the program itself, clearing a certain line, clearing certain characters etc. I'm writing a program that requires a certain type of clearance, so it would be very useful. I'll write a list in this post and append it as the thread goes along, so it ends up as a comprehensive list of options:

    Output clearing:

    '\b' -- backspace character, moves the cursor back one position, deleting any character that may reside in that position

    system("clear") -- stdlib function, used to execute a shell command, takes a string as an argument ("clear" invokes the clear command in the terminal)

    ---------------------------------------------------------------------------------------------------------------------

    Any other suggestions would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    FAQ > Clear the screen? - Cprogramming.com

    Quote Originally Posted by GabrielSaul View Post
    '\b' -- backspace character, moves the cursor back one position, deleting any character that may reside in that position
    FYI '\b' is only guaranteed to work on the current line.
    Last edited by Matticus; 04-28-2016 at 11:54 AM.

  3. #3
    Registered User
    Join Date
    Apr 2016
    Location
    London, UK
    Posts
    3
    Quote Originally Posted by Matticus View Post
    FAQ > Clear the screen? - Cprogramming.com



    FYI '\b' is only guaranteed to work on the current line.

    Of course, naturally.

    I've also noticed the effect of the backspace character can be temperamental, as it is "swallowed" by the terminal and at times doesn't have the desired. effect on the output depending on the scenario. Another reason why I'm looking for other options.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    ANSI escape sequences are fun (consider the portability issues, though). https://en.wikipedia.org/wiki/ANSI_escape_code
    Code:
    #include <stdio.h>
    
    #define CSI "\033["
    
    int main() {
        printf(CSI "2J");  // clear entire screen
        printf(CSI "H"); // move cursor to row 1, col 1 (one-based)
    
        for (int i = 0; i < 8; i++) // printing in different colors
            printf(CSI "%dma line of text blah blah blalalalalalah\n", i+30);
    
        getchar();
    
        printf(CSI "4;1H"); // move cursor to row 4, col 1
        printf(CSI "J"); // clear from cursor position to end of screen
    
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Apr 2016
    Location
    London, UK
    Posts
    3
    Quote Originally Posted by algorism View Post
    ANSI escape sequences are fun (consider the portability issues, though). https://en.wikipedia.org/wiki/ANSI_escape_code
    Code:
    #include <stdio.h>
    
    #define CSI "\033["
    
    int main() {
        printf(CSI "2J");  // clear entire screen
        printf(CSI "H"); // move cursor to row 1, col 1 (one-based)
    
        for (int i = 0; i < 8; i++) // printing in different colors
            printf(CSI "%dma line of text blah blah blalalalalalah\n", i+30);
    
        getchar();
    
        printf(CSI "4;1H"); // move cursor to row 4, col 1
        printf(CSI "J"); // clear from cursor position to end of screen
    
        return 0;
    }

    Interesting. Works quite well for what I'm trying to achieve.

    Can you explain the process behind it? Thanks a lot for the suggestion.

    I thought I might as well append the post and make it about a broader range of output manipulation methods, such as appending outputs. That's basically what I'm trying to do anyhow.

    My goal is to display a two-dimensional array as a 10x10 board and elements change throughout a loop. I figured the best method would be clearing the output on each iteration after pausing the program, then reprinting the new array with the new values. Although, there could be a better method, such as appending.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I'm not sure what you mean by the "process behind it". Did you read the wiki page? I doubt I know anything that isn't there! You can search for "ansi escape codes" for other sources. If you have any specific questions about the code I posted I'll answer them.

    For updating a single cell in your displayed 2d array you could position the cursor at the right row/col and overwrite the value there (ensuring what you write has the same number of characters so it completely overwrites the old value).
    Code:
    #include <stdio.h>
    
    #define SIZE 10
    #define FIELD_WIDTH 4
    #define CSI "\033["
    
    void clear_screen() { printf(CSI "2J" CSI "H"); }
    
    void clear_to_eol() { printf(CSI "K"); }
    
    void move_cursor(int row, int col) { // row/col are zero-based!
        printf(CSI "%d;%dH", row + 1, col + 1); // but are 1-based here
    }
    
    int main() {
        clear_screen();
    
        // make a grid of numbers
        for (int r = 1; r <= SIZE; r++) {
            for (int c = 1; c <= SIZE; c++)
                printf("%*d", FIELD_WIDTH, r * c);
            putchar('\n');
        }
    
        while (1) {
            int row, col, val;
            printf("Enter row col val: ");
            if (scanf("%d%d%d", &row, &col, &val) != 3)
                break;
    
            move_cursor(row, col * FIELD_WIDTH); // zero-based!
            printf("%*d", FIELD_WIDTH, val);
    
            move_cursor(SIZE, 0); // move cursor back to bottom
            clear_to_eol();
        }
    
        putchar('\n');
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is there really a better of the two options I have?
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 12-26-2007, 08:37 AM
  2. GCC binary output file options
    By Pyroteh in forum Linux Programming
    Replies: 1
    Last Post: 01-08-2007, 09:15 PM
  3. clearing output buffer?
    By epidemic in forum C++ Programming
    Replies: 2
    Last Post: 01-04-2007, 06:45 PM
  4. 2 options.... which way should I go?
    By CodeMonkey in forum Game Programming
    Replies: 9
    Last Post: 03-18-2002, 09:20 PM

Tags for this Thread