Is there some functions in c which behaves like scanf (taking inputs from the keyboard) while being able to move the cursor in the input by pressing the arrow keys, like in a terminal ?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
There are no function in C standard to do that.
You can look at ncurses
回答2:
In addition to previous suggestions, if you want to do a little work you can "roll your own" using the vt100 escape sequences, which most (probably all) terminal programs continue to support. Here's a small sample program,
#include <stdio.h>
#include <string.h>
#include "vt100.h"
int main ( int argc, char *argv[] )
{
char cupstr[99];
char reply[99];
strcpy(cupstr,_cup(12,25));
printf("%sEnter Data ---> ",cupstr);
fgets(reply,88,stdin);
strcpy(cupstr,_cup(13,25));
printf("%sEchoed Data --> %s\n",cupstr,reply);
}
And the big part is that vt100.h containing all the escape sequences,
/* ------------------------------------------------------------------------- *
VT100.H (c)1989-1999, John Forkosh Associates VT-100 Graphics
* ------------------------------------------------------------------------- */
#define bel printf("\007")
#define esc printf("\033")
#define csi printf("\033[")
#define lscreen printf("\033[?5h")
#define dscreen printf("\033[?5l")
#define rev_vid printf("\033[7m")
#define blink printf("\033[5m")
#define under printf("\033[4m")
#define bold printf("\033[1m")
#define norm_vid printf("\033[0m")
#define wide_vid printf("\033#6")
#define high_vid2 printf("\033#4")
#define high_vid1 printf("\033#3")
#define graphic printf("\033(0")
#define no_graph printf("\033(B")
#define no_window printf("\033[1;24r")
#define no_att printf("\033[0;22;24;25;27m")
#define cup(row,col) printf("\033[%d;%dH",(row),(col))
#define stbm(top,bot) printf("\033[%d;%dr",(top),(bot))
#define ri printf("\033M")
#define el printf("\033[K")
#define ed0 printf("\033[0J")
#define bar graphic;printf("x");no_graph
#define wipe(row,col) cup(row,col);printf("\033[J")
#define clr wipe(0,0);norm_vid
/* ------------------------------------------------------------------------- *
String equivalents ... Note: _cup() and _stbm() only accept literal args.
* ------------------------------------------------------------------------- */
#define _bel "\007"
#define _esc "\033"
#define _csi "\033["
#define _lscreen "\033[?5h"
#define _dscreen "\033[?5l"
#define _rev_vid "\033[7m"
#define _blink "\033[5m"
#define _under "\033[4m"
#define _bold "\033[1m"
#define _norm_vid "\033[0m"
#define _wide_vid "\033#6"
#define _high_vid2 "\033#4"
#define _high_vid1 "\033#3"
#define _graphic "\033(0"
#define _no_graph "\033(B"
#define _no_window "\033[1;24r"
#define _no_att "\033[0;22;24;25;27m"
#define _cup(row,col) "\033[" #row ";" #col "\110"
#define _stbm(top,bot) "\033[" #top ";" #bot "\162"
#define _ri "\033M"
#define _el "\033[K"
#define _ed0 "\033[0J"
#define _bar "\033(0x\033(B"
#define _wipe_clr "\033[0;0H\033[2J"
#define _clr "\033[0;0H\033[2J\033(B\033[0m\033[1;24r"
/* ------------------------------------------------------------------------- */
These escape sequences let you program all the stuff that used to look so cool on vt100's, vt220's, etc, a long, long time ago (but right here in this galaxy).