繁体   English   中英

C打印一行到控制台进行编辑

[英]C print a line to console to edit

是否可以将行打印到控制台并让用户对其进行编辑? 我将C用于控制台应用程序。

我试图给用户一个行形式的变量,然后让他/她对其进行编辑并将其保存回变量中。

如果不允许使用外部库,则我的方法是将终端设置为原始模式,显示字符串,然后逐字符读取直到找到返回值,并相应地调整显示的字符串。 即,对于每个退格键,删除最后一个字符,然后重新显示(使用\\r移至行首)。 我不会提供源代码,因为您应该在学校项目中学习一些东西:-)

PS:如果键入RETURN将光标放在下一行,则可能还需要调整字符的回显。

如果您确实需要一种可移植的方式,则必须记住旧的行编辑器过去的工作情况。

基本上有两种模式:

  • 打印一行,阅读命令并将命令应用到该行

     abcdefg s/cd/CD/ => abCDefg 

    s代表替代品

  • 打印该行,读取一个空格不改变且非空格替换的模式(较简单,但更难管理插入或删除):

     abcdefg CD => abCDefg 

嗅...那是七十年代....

为控制台(终端)编辑器提供以下示例代码。

编者

 #include <stdio.h> #include <stdlib.h> /*malloc(),free(),strtol()*/ #include <string.h> #include <ctype.h> /* isdigit() */ /* checking ,save as unicode(UTF-8) file format on M$*/ /* */ /* meta-comment for comment:don't erase */ /* 20050606 start making smoll,dum editor */ /* 20050612 working line-level ED(editor dummy):e1112.c*/ /* 20050612a making monitoring-line-level e131.c*/ /* 2008.6.19 LED(Line EDitor)-E13999.c */ /* 2008.6.20 line,word total number- space characters searching function procedure :isspace(),isalnum()... */ /* 2008.6.21 line ending function ;line add*/ /* 2008.6.28 line numbers sum resign on header portion */ /* 20080629 randum access function*/ /*2008.7.3 receive,data input once 3parameters, malloc(),free(),including */ /*20080726 random Access Memory Method editor stage 1, completed, malloc()*/ /*20080726 RAMeditor stage 2 completed including copy file making*/ /*alias=LED=edword=edward=rameditor */ /*20080801: pre(PRe-Editor)*/ /*20160314:MS-version exe file loading; Just in Time Running on execute this file(64bit)*/ /*160315: fedora linux tested good worked(32b)*/ /*how can I figure out to add line or delete line*/ /*why is this not working?--->check 64bit or 32bit*/ #define MAXLINE 5000 #define MAXLEN 1000 #define MINLEN 128 void StringCopy(char *, char *) ; void StringCopy2(char *, char *) ; void StringCopy3(char *, char *) ; void main(int argc, char *argv[]) { FILE *fin, *fout; char *buf[MAXLINE]; int i; char str[MINLEN]; char strs[MINLEN]; char *buf2[MAXLINE]; long length; int c; int nl=0; int str2; char **ptr; char name0[MINLEN]; char name1[MINLEN]; if(argc < 3) { printf("Hello world , I am pre-editor!!!\\n"); printf("using: E1*** original_file copy_file\\n"); //return -1; printf("now, type original_file name... %c \\n" , argv[0]); //scanf("%c",argv[1]); gets(name0); printf("type copy_file name... \\n"); gets(name1); printf(" pre_editor: open %c edite %c \\n",name0,name1 ); if((fin = fopen(name0, "r")) == NULL) { printf("original_file is not"); //return -1; exit( -1); }else{} if((fout = fopen(name1, "w+")) == NULL) { printf("copy_file is not"); //return -1; exit( -1); }else{} } else if (argc = 3){ if((fin = fopen(argv[1], "r")) == NULL) { printf("original_file is not-1"); //return -1; }else{} if((fout = fopen(argv[2], "w+")) == NULL) { printf("copy_file is not"); //return -1; }else{} } else { printf("using: E1*** original_file copy_file \\n 3parameter no more..."); } while (!feof(fin) ) { c = fgetc(fin); if (c =='\\n') ++nl; } fseek(fin,0,SEEK_END); length=ftell(fin); printf("------------<<RAM-LED>>--------------\\n"); printf("Total--%dLine(s),FP%ldByte(s)---\\n", nl,length); fseek(fin,0,SEEK_SET); printf("---<ctrl+m>-Enter--<ctrl+c>--Quit---\\n"); printf("---LED---L%d---B%ld----\\n", nl,length); printf("WARNING: RAMlocking using\\n");/*malloc()using*/ for(i=0 ; i<(nl+1); i++) { fgets(strs,128,fin); buf[i]= (char *)malloc(strlen(strs)+MINLEN); buf2[i]= buf[i]; StringCopy(buf[i],strs); printf("buf[%d]address: %p\\n",i,buf[i]); printf("buf[%d]all cell: %s\\n",i,buf[i]); } /*edward engine begine*/ printf("===================HELP===F1===============================\\n"); printf("|0| RAM---tracer--<<RAM-LED>>-- ||\\n"); printf("|1| No(out) funcion is just tap Enter key ||\\n"); printf("|2| also, ESC-->Enter(Key) ||\\n"); printf("|3| buf[NUMBER]<---type NUMBER,and tab Enter key to READ ||\\n"); printf("|4| checking and to re-edite -->use READ funcion ||\\n"); printf("===================HELP====================================\\n"); for(;;) { printf("Do you read??? NUMBER-->"); gets(str); /* scanf("%c", &str); */ if (str[0] == '\\0') { break; } else { str2=strtol(str,ptr,0); printf("buf[%d]address: %p\\n",str2,buf2[str2]); printf("buf[%d]all cell:%s\\n",str2,buf2[str2]); printf("Do you edit??? TEXT-->"); gets(str); if (str[0] == '\\0') { /*do nothing*/ } else { StringCopy2(buf[str2],str); StringCopy3(buf[str2],str); } } } /* for(;;) ending*/ /*edward engine ending*/ /*destination file copy completed, SKADOOSH,SKADOOSH,skadoosh kungfu penda3*/ /*fputs(w+)using*/ printf("RAM data\\n"); for(i=0 ; i<(nl+1); i++) { printf("buf[%d]address: %p\\n",i,buf2[i]); printf("buf[%d]all cell: %s\\n",i,buf2[i]); fputs(buf2[i], fout); } /*free()using*/ printf("RAM address\\n"); for(i=0 ; i<(nl+1); i++) { free(buf2[i]); printf("buf[%d]address: %p\\n",i,buf[i]); printf("buf[%d]all cell: %x\\n",i,buf[i]); } fclose(fin); fclose(fout); printf("===END===\\n"); }/* main() ending*/ void StringCopy(char *dst, char *src) { while(*src) *dst++=*src++; *dst='\\0'; } void StringCopy2(char *dst, char *src) { /*GC :garbages are made in here*/ /* char *dst2; *dst2 = *dst; */ while(*dst){ *dst++ = '\\0'; } } void StringCopy3(char *dst, char *src) { /* *dst = *dst2; */ while(*src) { *dst++=*src++; *dst='\\n'; } } void help () {//not yet figured out /*alert, F1~, key event binding value, 112~*/ printf("===================HELP===F1===============================\\n"); printf("|0| RAM---tracer--<<RAM-LED>>-- ||\\n"); printf("|1| No(out) funcion is just tap Enter key ||\\n"); printf("|2| also, ESC-->Enter(Key) ||\\n"); printf("|3| buf[NUMBER]<---type NUMBER,and tab Enter key to READ ||\\n"); printf("|4| checking and to re-edite -->use READ funcion ||\\n"); printf("===================HELP====================================\\n"); } /* ===END===(EOF)*/ 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM