是否可以在控制台上打印一行并让用户进行编辑?我使用C作为控制台应用程序。
我试图给用户一个变量行,让他/她编辑它并将其保存回变量。
答案 0 :(得分:4)
如果不允许外部库,我的方法是将终端设置为原始模式,显示字符串,然后逐个字符地读取,直到找到返回,相应地调整显示的字符串。即为每个退格键删除最后一个字符,然后重新显示(移动到\r
的行首)。我不打算提供源代码,因为你应该在你的学校项目中学到一些东西: - )
PS:如果键入RETURN将光标放在下一行,您可能还需要调整字符的回显。
答案 1 :(得分:1)
如果你真的需要一种可移植的方式,你必须记住以前的行编辑有多好用。
基本上有两种模式:
打印一行,读取命令并将命令应用到行
abcdefg
s/cd/CD/
=> abCDefg
其中s
代表替代
打印该行,读取空格意味着不改变和非空格替换的模式(更简单,但更难管理插入或删除):
abcdefg
CD
=> abCDefg
答案 2 :(得分:0)
在控制台(终端)编辑器的示例代码下面。
#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)*/
&#13;