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

C print a line to console to edit

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

我正在尝试为用户提供一行形式的变量,然后让 him/her 对其进行编辑并将其保存回变量中。

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

PS:如果键入 RETURN 将光标置于下一行,您可能还需要调整字符回显。

如果您真的需要一种可移植的方式,您必须记住旧的行编辑器曾经工作得多么好。

基本上有两种模式:

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

    abcdefg
    s/cd/CD/
    => abCDefg
    

    其中s代表替代

  • 打印该行,读取一个模式,其中 space 表示不更改且非 space 替换(更简单,但更难管理插入或删除):

    abcdefg
      CD
    => abCDefg
    

闻...那是七十年代....

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

前editor.c

#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] == '[=10=]') {
                              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] == '[=10=]') {
                                          
                    /*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='[=10=]';
}
void StringCopy2(char *dst, char *src) {  /*GC :garbages are made in here*/
/* char *dst2;
  *dst2 = *dst; */
while(*dst){ 
  *dst++ = '[=10=]';
  }
}
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)*/