开发者

how to print to console graphically in C on unix

So I'm familiar with printf and the like, but I need to update a single line on the screen without having multiple lines scrolling. I have found libraries to do this in windows (conio.h) but not in unix. I need to be able to run this in cygwin, but any unix examples would be welcome.

I开发者_JAVA技巧 found the following stackoverflow article , but I don't think it quite closes the question for my needs.

Thanks for your help


It depends on whether you're trying to do a text-mode full-screen application (in which case ncurses is probably what you want) or just want to update a single line in-place (e.g., you want to update an "X percent done" indicator from 1 to 100, with all the output appearing on the same line so when X percent done is printed, it prints "over" the previous X-1 percent done indicator). In the latter case, you can write code that's relatively portable, and considerably simpler as well. For example, something like this:

#include <windows.h>    // Used only for "Sleep" in our simulated work load
#include <stdio.h>

void do_work() {
    // Simulated work load. Just waste some time:
    Sleep(100);
}

int main() {
    for (int i=0; i<100; i++) {
        char buffer[82];
        sprintf(buffer, "%d percent done", i+1);
        printf("\r%-79s", buffer);
        do_work();
    }
    return 0;
}


You want Ncurses for this. It's a library which allows you to edit any character on the screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜