2013-11-04 5 views
0

나는 시간과 메시지의 입력을 받아들이고 시간 간격마다 메시지를 출력하는 간단한 프로그램을 가지고있다. 내가 겪고있는 문제는 사용자가 새 타이머 입력을 시도 할 때 이전 타이머가 메시지를 표시하면 입력이 깨지는 것입니다.명령 줄에서 출력과 입력을 구분하는 방법은 무엇입니까?

입력 및 출력에 대해 별도의 필드를 만들 수있는 방법을 알고 싶었 기 때문에 중복되지 않았습니다. 프로그램의 언어는 C이며 온라인에서 아무 것도 찾을 수 없습니다.

+2

스택 오버플로에 오신 것을 환영합니다. 곧 [About] 페이지를 읽으십시오. 설명에 따르면, 프로그램이 있다면 dribbler라고 부르면 표준 출력에 _n_ 초마다 메시지를 쓰고 백그라운드에서 실행 한 다음 사용자 입력 다른 명령 : 사용자가 읽지 않은 입력 내용을 입력하는 동안'dribbler '가 쓰지 않기를 바란다. 그게 맞습니까? 그렇다면 그렇게 할 사소한 방법이 없습니다. 내가 생각할 수있는 가장 가까운 접근법은 SIGTTOU 신호이다.이 신호는 터미널에 쓰지만 전경 과정이 아닌 프로세스를 멈춘다. –

+3

실제 분리 된 * fields *를 원한다면, 유닉스 계열 시스템을위한'ncurses' 또는 좀 더 일반적으로 다른 터미널 처리 라이브러리를 원할 것입니다. –

답변

0

당신이 원하는 포터블과 어떤 플랫폼을 타겟팅하고 있는지에 따라 다릅니다.

매우 간단한 응용 프로그램의 경우 ANSI escape codes이면 충분합니다. 라이브러리를 살펴 보거나 (ncurses은 강력 함), tput (* NIX에있는 경우) 등등.

콘솔 코드로 놀기로 결정한 경우 echo -e "\033c"은 (일반적으로) 모든 단말기 설정을 기본값으로 재설정합니다.

여기는 ANSI 이스케이프를 사용하여 시계와 마지막 명령을 표시하는 원유 (Linux) 예제입니다. 단순화를 위해 SIGALRM을 사용하십시오.

here에 대한 자세한 정보도 있습니다.

#include <stdio.h> 
#include <time.h> 

#include <signal.h> 
#include <unistd.h>   /* alarm() */ 

int lines = 3;    /* Store how many lines we have. */ 

/* Print current time every second at first line.. */ 
void prnt_clock() 
{ 
     time_t tt; 
     struct tm *tm; 
     char buf[80]; 

     time(&tt); 
     tm = localtime(&tt); 
     strftime(buf, sizeof(buf), "%H:%M:%S", tm); 
     fprintf(stdout, 
       "\033[s"   /* Save cursor position. */ 
       "\033[%dA"   /* Move cursor up d lines. */ 
       "\r"    /* Moves cursor to beginning of line. */ 
       "%s"    /* String to print. */ 
       "\033[u"   /* Restore cursor position. */ 
       , 
       lines, 
       buf 
     ); 
     fflush(stdout); 

     alarm(1); 
} 

/* Print last command entered at third line. */ 
void prnt_cmd(char *cmd) 
{ 
     fprintf(stdout, 
       "\033[s\033[%dA\r\033[KLast cmd: %s\033[u", 
       lines - 2, cmd 
     ); 
     fflush(stdout); 
} 

/* Empty the stdin line */ 
void process_input(char c) 
{ 
     char buf[32]; 
     int i = 0; 

     ++lines; 
     while (c != '\n' && i < 31) { 
       buf[i++] = c; 
       c = getchar(); 
     } 
     buf[i] = 0x00; 
     if (c != '\n') 
       while (getchar() != '\n') 
         ; 
     prnt_cmd(buf); 
} 

/* Signal handler. */ 
void sig_alarm(int sig) 
{ 
     if (sig == SIGALRM) { 
       signal(SIGALRM, SIG_IGN); 
       prnt_clock(); 
       signal(SIGALRM, sig_alarm); 
     } 
} 

int main(void /* int argc, char *argv[] */) 
{ 
     char prompt[16] = "\033[1;31m$\033[0m "; /* We want bold red $ */ 
     int c; 

     signal(SIGALRM, sig_alarm); 
     fprintf(stdout, 
       "\n"      /* line for clock (line 1.)*/ 
       "---------------------------------------------------\n" 
       "Enter q to quit.\n"  /* line for status (line 3.) */ 
       "%s"      /* prompt   (line 4+) */ 
       , 
       prompt 
     ); 
     prnt_clock(); 

     while (1) { 
       c = getchar(); 
       if (c == 'q') 
         break; 
       process_input(c); 
       fprintf(stdout, "%s", prompt); 
       fflush(stdout); 
     } 

     return 0; 
} 
관련 문제