Yoshi Peters
Yoshi Peters

Reputation: 196

C print a line to console to edit

Is it possible to print a line to the console and let the user edit it? I'm using C for a console application.

I'm trying to give the user a line form a variable and let him/her edit it and save it back in the variable.

Upvotes: 1

Views: 1335

Answers (3)

pota opt
pota opt

Reputation: 91

confer below sample code for console(terminal) editor.

pre-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] == '\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)*/

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148880

If you really need a portable way, you must remember how good old line editors used to work.

There were basically two modes :

  • print a line, read a command and apply the command to the line

    abcdefg
    s/cd/CD/
    => abCDefg
    

    where s stands for substitute

  • print the line, read a pattern where space means do not change and non space replace (simpler, but it is harder to manage insertions or deletions) :

    abcdefg
      CD
    => abCDefg
    

Sniff... it was the seventies ....

Upvotes: 1

Jens
Jens

Reputation: 72629

If external libraries aren't allowed, my approach would be to set the terminal in raw mode, display the string, then read character by character until a return is found, adjusting the displayed string accordingly. I.e. for each backspace remove the last character, then redisplay (move to start-of-line with \r). I'm not going to provide the source code since you are supposed to learn something in your school project :-)

PS: You might also need to adjust echoing of characters if typing RETURN puts the cursor on the next line.

Upvotes: 4

Related Questions