Output program file and comment in the deletion program

Source: Internet
Author: User

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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.