Scrolling text in C

烈酒焚心 提交于 2019-12-25 12:37:13

问题


I would like to scroll a text continuously in the screen. For example,

text = "Hello, how are you"

The output should be:

Hello, how are you Hello, how are you
Hello, how are you Hello, how are you

and rotating from right to left.

So far I have compiled this:

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()

int main(int argc, char *argv[]) {

    char *text = argv[1];
    char *text = "Hello, how are you";
    int text_length;
    int i, max_x, max_y;

    // Get text length
    text_length = strlen(text);

    // Initialize screen for ncurses
    initscr();
    // Don't show cursor
    curs_set(0);
    // Get terminal dimensions
    //   getmaxyx(stdscr, max_y, max_x);
    // Clear the screen
    clear();

    // Scroll text back across the screen

    while (1) {
        getmaxyx(stdscr, max_y, max_x);

        if ((max_x - text_length) <= 1)
            i = max_x;
        else
            i = (max_x - text_length);

        for (i; i > 0; i--) {
            clear();

            mvaddstr(0, i, text);
            refresh();
            usleep(20000);
        }
    }
    // Wait for a keypress before quitting
    getch();

    endwin();

    return 0;
}

Can anybody help to change the code and do that?


回答1:


Your problem was intriguing enough I couldn't stop until I'd made it "work":

#include <curses.h>
#include <unistd.h> // For sleep()
#include <string.h> // For strlen()
#include <stdlib.h> // For malloc()

int main(int argc, char* argv[])
{

    char *text = "Hello, how are you? ";
    char *scroll;
    int text_length;

    int i, max_x, max_y;

    // Get text length
    text_length = strlen(text);

    // Initialize screen for ncurses
    initscr();
    // Don't show cursor
    curs_set(0);
    // Get terminal dimensions
    //   getmaxyx(stdscr, max_y, max_x);
    // Clear the screen
    clear();

    getmaxyx(stdscr, max_y, max_x);
    scroll = malloc(2 * max_x + 1);

    for (i=0; i< 2*max_x; i++) {
            scroll[i] = text[i % text_length];
    }

    scroll[2*max_x - 1]='\0';


    // Scroll text back across the screen
    for (i=0; i < 10000; i++) {

            mvaddnstr(0,0,&scroll[i%max_x], max_x);
            refresh();
            usleep(20000);
    }
    // Wait for a keypress before quitting
    getch();

    endwin();

    return 0;
}

Note that I've cheated :) (a) I duplicate the string to more than large enough to always fill the screen (twice the width) (b) I don't scroll the print location, I scroll the text that I ask to print (c) I just put a space in the original input string because it was easier than putting in a space through another mechanism.

Oh yeah, I removed the clear() call, it made the screen too messy to really see. We're over-writing the same max_x cells over and over though, no need to keep clearing the entire screen.

I hope this helps.




回答2:


Just use an index into the array and output one character with putchar() at the correct screen location.Try it take a look at this link may be it will help u out http://www.dreamincode.net/code/snippet1964.htm




回答3:


 1. 

    /*this one is easy to understan*/


                #include <stdio.h> 
            #include <conio.h> 
            #include <string.h> 
            //library for sleep() function 
            #include <unistd.h> 
            void main() 
            { 
                int i,j,n,k; 

            //text to be srolled 
                  char t[30]="akhil is a bad boy"; 

            n=strlen(t); 

                for(i=0;i<n;i++) 
                 { 
                    printf("\n"); 

                   //loop for printing spaces 

                      for(j=20-i;j>0;j--) 
                         { 
                                printf(" "); 
                         } 

                  /*printing text by adding a                  character every time*/ 

                 for(k=0;k<=i;k++) 
                  { 
                      printf("%c",t[k]); 
                   } 

            // clearing screen after every iteration 

            sleep(1); 
            if(i!=n-1) 
            clrscr(); 
               }   
 }


来源:https://stackoverflow.com/questions/5139892/scrolling-text-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!