A simple text compiler, simple text Compiler
It took a few days, from no idea to writing. Because this is the first time to write such commands interactively, there are many bugs.
Format: E/e: Specify the file to be edited
Q/q: finish editing
R/r (replace the m row to n rows in the original text with the k-line text after the r command)
R k m n
K-line text
I/I (insert the k-line text after the I command to the m-line text after the original text)
I k m
K-line text
D/d (delete the content in the text line m to line n from the original body)
D m n
Here is the code I wrote:
1/* implement a simple text editor */2/* bug1: Press e first and then file name. After the loop, ch is equal to '\ n' */3/* bug2: in the insert function, if the last line of the body is not the carriage return, the first line to be inserted is not separated on the Inserted Line */4 # include <stdio. h> 5 # include <stdlib. h> 6 # include <string. h> 7 8 # define CHMAX 80/* a row can contain up to 80 characters */9 # define LINEMAX 200/* a maximum of 200 lines */10 11 char command [] = "EeRrIiDdQq"; /* optional command */12 char buffer [CHMAX];/* input buffer */13 char * Line [LINEMAX];/* row pointer, used to input a row */14 char * chpt;/* to get the life cycle Character pointer */15 char filename [256];/* input file name */16 int modified = 0;/* modify flag */17 int last; /* the last line of the text, no content */18 void Edit (), Replace (), Insert (), Delete (), Quit (); 19 void (* comfunc []) () = {Edit, Replace, Insert, Delete, Quit};/* function pointer array */20 void call (); 21 22 int main (void) 23 {24 call (); 25 return 0; 26} 27 28 void call () 29 {30 int j = 0; 31 char ch; 32 last = 0; 33 34 while (1) 35 {3 6 j = 0; 37 printf ("Input a command: [e, r, I, d, q] \ n "); 38 39/* bug1 */40 // ch = ''; 41 // fflush (stdin); 42 while (ch = fgetc (stdin ))! = '\ N')/* enter the command */43 {44 buffer [j] = ch; 45 j ++; 46} 47 buffer [j] =' \ 0 '; 48 49 for (chpt = buffer; * chpt = ''| * chpt = '\ T'; chpt ++)/* skip the blank space */50; 51 52 if (* chpt = '\ 0') 53 {54 printf ("Doesn't input any command \ n"); 55 continue; 56} 57 58 for (j = 0; command [j]! = * Chpt & command [j]! = '\ 0'; j ++)/* obtain the command serial number */59 if (command [j] =' \ 0 ') 60 {61 printf ("Cannot find command. \ n "); 62 continue; 63} 64 chpt ++;/* point to parameter */65 (* comfunc [j/2]) (); /* execute The corresponding function */66 67 printf ("The text is: \ n"); 68 for (j = 0; j <last; j ++) /* output text content */69 fputs (Line [j], stdout); 70} 71 72} 73 74 void Edit () 75 {76 int I; 77 FILE * fp_temp;/* temporary FILE pointer */78 79 I = sscanf (chpt, "% s", filename);/* ssc The Return Value of the naf function is the number of parameters. The parameter must have only one */80 81 if (I! = 1)/* no input file name or more than one file name */82 {83 printf ("not input filename or more than 1 file, please input filename again: \ n "); 84 scanf ("% s", filename); 85} 86 87/* Open the specified file, create a new file */88 if (fp_temp = fopen (filename, "r") = NULL)/* if this file does not exist, create a new file */89 {90 if (fp_temp = fopen (filename, "w ")) = NULL)/* create a new file */91 {92 printf ("Cannot create a new file"); 93 return; 94} 95 fclose (fp_temp); 96 I F (fp_temp = fopen (filename, "r") = NULL) 97 {98 printf ("Cannot read"); 99 return; 100} 101} 102 103 I = 0; 104 while (fgets (buffer, CHMAX, fp_temp) = buffer)/* do not worry about buffer out-of-bounds issues, because buffer */105 {106 Line [I] = (char *) malloc (strlen (buffer) + 1); 107 strcpy (Line [I], buffer); 108 I ++; 109} 110 last = I; 111 112 fclose (fp_temp); 113 modified = 1; /* modified */114} 115 116 void Replace () 117 {118 I Nt I, k, m, n;/* Replace the text of Line k with the text of line m and line n */119 120 if (modified! = 1) 121 {122 printf ("Did not open a file. \ n "); 123 return; 124} 125 126 I = sscanf (chpt," % d ", & k, & m, & n ); 127 if (I! = 3 | m <0 | n <0 | k <0 | m> n | n> last | k> last) 128 {129 printf ("format error \ n"); 130 return; 131} 132 133 134 if (k! = (N-m + 1) 135 {136 printf ("error number of lines replaced. \ n "); 137 return; 138} 139 140/* write k lines of text */141 printf (" Please input % d line words: \ n ", k ); 142 143/* replace */144 for (I = 0; I <k; I ++) 145 {146 free (Line [m + I-1]); 147 fgets (buffer, CHMAX, stdin); 148 Line [m + I-1] = (char *) malloc (strlen (buffer) + 1); 149 strcpy (Line [m + I-1], buffer ); 150} 151 152 modified = 1; 153} 154 155 void Insert () 156 {157 int I, k, M;/* Insert the text of k rows into the original text after the m row */158 159 160 if (modified! = 1)/* Edit last modified flag */161 {162 printf ("Did not open a file. \ n "); 163 return; 164} 165 166 I = sscanf (chpt," % d ", & k, & m); 167 168 if (I! = 2 | k <0 | m <0 | last + k> = LINEMAX) 169 {170 printf ("format error. \ n "); 171 return; 172} 173 174/* write k lines of text */175 printf (" Please input % d line words: \ n ", k ); 176 177/* first move the Row m + 1 of the original text to backward k rows */178 for (I = last-1; I> s-1; I --) 179 {180 Line [I + k] = Line [I]; /* Move k rows to the backend */181} 182 183/* and add the k rows in the middle */184 for (I = 0; I <k; I ++) 185 {186 fgets (buffer, CHMAX, stdin); 187 Line [m + I] = (char *) malloc (strlen (bu Ffer) + 1); 188 strcpy (Line [m + I], buffer); 189} 190 191 last + = k; /* Add k rows to the original last vertex */192 modified = 1;/* modified */193} 194 195 void Delete () 196 {197 int I, j, m, n;/* Delete the content from Row m to row n of the original body */198 199 if (modified! = 1) 200 {201 printf ("Did not open a filen \ n"); 202 return; 203} 204 205 I = sscanf (chpt, "% d ", & m, & n); 206 if (I! = 2 | m <0 | n <0 | m> n | n> last) 207 {208 printf ("format error \ n"); 209 return; 210} 211 212/* Delete first, then shift */213 for (I = 0; I <(n-m + 1); I ++) 214 free (Line [m + I-1]); 215 for (I = n, j = 0; I <last; I ++, j ++) 216 Line [m + J-1] = Line [I]; 217 last-= (n-m + 1); 218 219 modified = 1; 220} 221 222 void Quit () 223 {224 int I; 225 char flag; 226 FILE * fp_temp; 227 228 printf ("Save? Y/n: "); 229 scanf (" % c ", & flag); 230 231 if (flag = 'y' | flag = 'y ') 232 {233 if (fp_temp = fopen (filename, "w") = NULL) 234 {235 printf ("Cannot open a file"); 236 exit (1 ); 237} 238 for (I = 0; I <last; I ++) 239 fputs (Line [I], fp_temp); 240 printf ("save successfully! \ N "); 241} 242 else243; 244 exit (0); 245 246}View Code
The running result is as follows:
Ps: it is best to have a carriage return key for each row, because the fgets function specifies the number of characters. For details, see the code.
If you are interested, you can improve it!