We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
IN C Starndard lib, use gets can input string form stdio.
for example, push [backspace] can remove the last char.
But when i use gets(), it keep backspace char (0x0B).
Of course gets() and fgets() retains the backspace if they receive a backspace. Backspace is just a character, and it isn't the transfer functions that should try to figure out destructive/nondestructive delete, or arrow keys etc.
If the sending side sends character-by-character, then you will have to process the backspace in your code. If the sending side sends full lines, then the sending side may perform backspace processing before sending data to you.
But gets()/fgets() does not, and should not, have any backspace processing.
"Not working correctly" means that you have documentation claiming that a function should work in a specific way, and you then find that it doesn't. Now please inform me where you have found documentation that says gets()/fgets() should process backspaces.
I just refenece VC++ document. So you right.
I should write a func to handle it, like this. char *getstring(char *string) { char *pointer; INT ch;
pointer = string; while (1) { ch = _getchar_lk(); if (ch == EOF) { return NULL; }
if (ch == 8) { if (pointer > string) { putchar('\b'); putchar(' '); putchar('\b'); pointer--; } } else if (ch == '\r' || ch =='\n' || ch >= ' ') { putchar(ch); if (ch == '\r') putchar('\n'); if (ch == '\n' || ch == '\r') break; *pointer++ = ch; } }
*pointer = 0; return string; }
Any reason why you missed the instruction how to post source code? Notice any difference in readability?
char *getstring(char *string) { char *pointer; INT ch; pointer = string; while (1) { ch = _getchar_lk(); if (ch == EOF) { return NULL; } if (ch == 8) { if (pointer > string) { putchar('\b'); putchar(' '); putchar('\b'); pointer--; } } else if (ch == '\r' || ch =='\n' || ch >= ' ') { putchar(ch); if (ch == '\r') putchar('\n'); if (ch == '\n' || ch == '\r') break; *pointer++ = ch; } } *pointer = 0; return string; }
By the way - what happens if you have already received characters that you have stored in your string when you get an EOF?
Another thing - exactly how large is your string? What happens if someone enters moer characters than what will fit in string?
Remember that VC++ documentation will deal with the specific VC++ implementation; you should not assume that what it says is generally applicable to all implementations - especially embedded cross-compiler implementations!
I don't think the VC++ documentation claims such behaviour either, since such behaviour is not part of the C standard and platform-specific behaviour should only be introduced when the platform so needs.
I can't find anything in my VC++ documentation.
But as I did mention - when the sending side handles the data line-by-line instead of character-by-character, it can process the backspace character so you may see this behaviour in a test program even if it isn't gets()/fgets() that is doing the backspace processing.