The function of a simple line editing program is to accept the program or data entered by the user from the terminal and deposit it into the user's data area. As the user in the terminal input, there is no guarantee that error, so, in the editing program, "Each accept a character is stored in the user data area" is clearly not the most appropriate. It is a good practice to set up an input buffer to accept a line of characters entered by the user, and then deposit the user data area row by line. Allow users to enter errors and correct them when they are found to be incorrect. For example, when a user discovers that a line that has just been typed is more or more difficult to remediate, you can type a "@" in a retrograde character to indicate that the characters in the current line are not valid. For example, suppose that two lines of characters are accepted from the terminal:
whli# #ilr #e (s#*s)
[Email protected] (*s=#++);
To do this, the input buffer can be set to a stack structure, each time a character from the terminal to do the following discrimination, if it is neither backspace nor a degenerative character, the character is pressed into the top of the stack; if it is a backspace, a character is deleted from the top of the stack; if it is a degenerative character, the word Fu Yiging is empty.
typedef CharSelemtype; FILE *FP;voidCopy (Selemtype c) {//Send the character C to the file referred to in the FPFPUTC (c, FP);}voidLineEdit () {//Use the word Fu Yi s to receive a row from the terminal and send it to the data area of the calling processSqstack s;CharCh Initstack (s);printf("Please enter a text file, ^z end input: \ n"); ch = getchar (); while(ch! = EOF)//When full text is not finished (EOF is ^z key, full-text terminator){ while(ch! = EOF && ch! =' \ n '){//When the full text is not finished and is not at the end of the line (not a newline character) Switch(CH) { Case ' # ':if(! Stackempty (s)) Pop (S, ch);//Only when the stack is not empty pop up the top element, C can be replaced by CH Break; Case ' @ ': Clearstack (s); Break;default:P ush (S, ch); Break; } ch = GetChar (); }//To end of line or full text, exit this layer loopStacktraverse (S, copy);//Transfer the stack characters from the bottom of the stack to the top of the stack sequentially to the file (call the copy () function)FPUTC (' \ n ', FP);//Enter a line break to the fileClearstack (s);//Reset S to empty stack if(ch! = EOF)//Full text not closedch = getchar (); } destroystack (s);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Line editing program