Pre-populate stdin in C

My program should allow the user to edit the file line. The user edits the line and sends it back by pressing enter. Therefore, I would like to print the current line that needs to be edited, but print it to stdin instead of stdout. The only problem I don’t know how to solve is how I can pre-populate stdin. I have already tried this:

char cprefill[] = {"You may edit this line"};
char cbuffer[100];
fprintf(stdin, cprefill);
fgets(cbuffer, 100, stdin);

This seems to be the easiest solution, but probably too easy to work with. Fprintf does not print anything for stdin. What is the right way?

Edit: Result

Here's how it should look. Pay attention to the cursor that can be moved.

+4
source share
2 answers

libreadline. (, libreadline, )

Debian/Ubuntu, apt install libreadline-dev ( libreadline6, , - 6 )

readline, ,

#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>    

...

char cprefill[] = {"You may edit this line"};

add_history(cprefill);

char *buf = readline("Line: ");

printf("Edited line is %s\n", buf);

// free the line allocated by readline
free(buf);

":", UP ARROW, , .. cprefill.

, / -lreadline

readline , , , , .

char *, readline, ( malloc()).

+1

C , . , [n] curses, , , .

, Windows , (, WM_CHAR) , - , , C, Windows...

+3

Source: https://habr.com/ru/post/1689625/


All Articles