Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The following program will clear the screen

#include <stdio.h>
int main()
{
    fputs("\033[2J", stdout);
    return 0;
}

So, what should I modify/add in the above code to put a text string in the middle of screen?

Note: screen size is: rows=25, columns=80

share|improve this question

closed as off topic by Caleb, Renan, Ulrich Dangel, jasonwryan, manatwork Sep 7 '12 at 7:45

Questions on Unix & Linux Stack Exchange are expected to relate to Unix or Linux within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
Please note that programming questions generally belong on Stack Overflow (see our faq). Unix & Linux is mainly for users and administrators, not programmers. Here, it's likely that we'll tell you what API to use (yup, curses); but help with this C API is within the domain of SO, not U&L. –  Gilles Jul 15 '11 at 11:04
    
@Gilles: Sorry, i thought question is specific to Linux only so this the best place to post. –  MA1 Jul 15 '11 at 11:16

3 Answers 3

In order to put text in the centre of the screen, you need to know a = how wide the text you are printing is (in visible character-terms); b = how wide the screen is (in visible character terms); then print (b/2 - a/2) buffering space characters before the string.

This kind of logic, and other related bits, are all handled in the curses library. I suggest you investigate using it.

share|improve this answer
    
rows=25, columns=80 –  MA1 Jul 15 '11 at 11:19
2  
Always? Are your users never going to resize the terminal? –  jmtd Jul 16 '11 at 10:17
    
no liberty to resize the terminal –  MA1 Jul 18 '11 at 8:03

Here's a rough method that works for you, you will need to first retrieve terminal size, and calculate the position of the string:

#include <iostream>
#include <cstring>
#include <sys/ioctl.h>

using namespace std;

void output_middle (const char *s, int term_cols)
{
    cout << string ( (term_cols - strlen(s)) >> 1, ' ') << s << endl;
}

int main ( int argc , char **argv ) 
{
    struct winsize w;
    ioctl(0, TIOCGWINSZ, &w);

    output_middle ("some string", w.ws_col);
    return 0;
}
share|improve this answer
#include <stdio.h>
#include <string.h>


static void print_middle(int rows, int cols)
{
    int vt = rows/2,
        ht = 0;

    char *message = "Please wait, Copying from network ...";

    ht = (cols - strlen(message))/2;

    fputs("\033[2J", stdout);

    printf("%c[%d;%df", 0x1B, ht, vt);

    printf("%s", message);

}

int main()
{
    print_middle(25, 80);

    return 0;
}    
share|improve this answer
    
Does this work with all possible terminal types? –  jmtd Jul 16 '11 at 10:18
    
This puts the cursor at position (22,12), the question was about centering text. And although these particular will work on practically any existing terminal, ncurses is better because it has other useful features (such as knowing how to get the terminal size, computing the display width of a string and so on). –  Gilles Jul 16 '11 at 13:38
    
@jmtd: should work perfectly on every VT100 supported terminal. –  MA1 Jul 18 '11 at 7:53
    
@Gilles: pasted the wrong code. Updated the question and the solution also. –  MA1 Jul 18 '11 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.