I remember I wrote an article about the output program file in my previous article. Today I want to expand this article.
When writing a program, programmers usually add some comments to make it easier to understand the program later.
There are two types of annotations in C/C ++:
(1) // comment content
(2)/* Comment [\ n] */
Next we will analyze these two situations to see how to delete these comments.
In the first case, we can write a comment starting with two double slashes, as long as we continue to judge whether the next one is a '/' when reading a character.
If yes, the subsequent parts are comments. Because of the single line content annotated in this way, we need to read the file until a line break is encountered;
If not, we need to determine whether the next character is a '*'. If yes, it belongs to the second annotation method, at this point, the end of the comment should be a "*/", that is, the adjacent two characters are '*' and '/' in sequence '/'.
Based on the simple analysis above, you can write the following C Language Program:
# Include <stdio. h> # include <stdlib. h> int main () {file * file = fopen ("Main. C "," R "); If (file = NULL) {perror (" Main. C "); // return 0;} Char CH, TMP = '\ 0';/* adnaiofa */while (CH = fgetc (File ))! = EOF) {If (CH = '/') {CH = fgetc (File); If (CH = '/') {While (CH = fgetc (File ))! = '\ N'); fputc (CH, stdout); continue;} else if (CH =' * ') {While (CH = fgetc (File ))) {If (TMP = '*' & Ch = '/') {break;} TMP = CH;} continue ;}} fputc (CH, stdout );} fclose (File); Return 0 ;}
Output result:
From the running result, we can see that the comments in the original program file have been deleted...