-1

when I try to read a string with fgets( line, MAX, stdin), I cannot use the arrows to move back and make correction; characters such as ^[[D are inserted instead. Is there a valid alternative to fgets.

This is the cycle I use to read the input:

char *buf = line;
char line[MAX];
printf("Enter x to quit\n>>");

for(;;) {

    if(fgets(buf = line, MAX, stdin) == NULL || *buf == 'x')
        break;

}

When *buf = '^[[D ' is there a way to move the cursors left like the backspace but without erasing previous characters?

I'd like a solution that doesn't resort on external libraries such as bash readline but purely C ANSI.

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

1 Answers1

1

The fgets function receives a string from the input stream. The relation between keystrokes and the input stream is entirely up to your operating system.

If the features provided by the OS are not suitable for you then you can use a console input library such as ncurses.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • 1
    Hmmm, in one way this is the correct answer, but OP specifically said no to external libraries – klutt Nov 05 '19 at 09:47