Yeah, makes sense now. A little time out of the office, a little food in my belly, and I think I'm (mostly) up and running. grumpy showed you a way using ungetc, but there's no guarantee that you can push back more than 1 character. Here's another hack alternative:
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int i;
char *p;
char ans[80];
char *prompt = "Enter some text";
char *prev_ans = "foo";
printf("%s: %s", prompt, prev_ans);
for (i = 0; i < strlen(prev_ans); i++) {
putchar('\b');
}
fgets(ans, sizeof(ans), stdin);
if (strcmp(ans, "\n") == 0) {
// empty string, use prev_ans
strcpy(ans, prev_ans);
}
else {
// trim trailing new line from ans
p = strchr(ans, '\n');
if (p != NULL) {
*p = '\0';
}
}
printf("The answer entered was: %s\n", ans);
return 0;
}
I'm not sure how standard or portable the behavior of printing those \b chars will be. There's also no way to clear the previous answer from the prompt once they start typing, unless you use raw terminal mode, but it's somewhat close. If you have to do a lot of this, or want it to be more professional, then perhaps look into the curses/ncurses library, or go back to the termios stuff.
Hopefully I'm on the right page this time.