By writing a C language program, the runtime implementation prints the source code and line number of another program

Source: Internet
Author: User

June 1, 2017 program preparation notes:

1. Implement the line number printing, realize the code reading and output, understand the parameter meaning in the main function.

2. Insufficient understanding of the Fgets function

3. To return (1); The meaning of return 0 is not understood enough

4. Printed page numbers are not implemented

1 /*************************************************************************2 > File name:my_print_program.c3 > Author:Mr.Yang4 > Purpose: Write your own program to print the source code and line number of a program (that is, run./my_print_program my_print_program.c You can print the source and line numbers to achieve5 features similar to Cat MY_PRINT_PROGRAM.C features in Linux)6 > Created time:2017 June 01 Thursday 15:38 33 seconds7  ************************************************************************/8 9#include <stdio.h>Ten#include <stdlib.h> One  A /*implementing the path and related parameters for reading executable programs from the command line*/ - intMainintargcChar*argv[]) - { the         Charbuffer[ the]; -FILE *FP; -  -         /*Make argc judgments*/ +         if(ARGC <2) -         { +Puts"\ n Please follow the standard command input, for example: \ "./my_print_program test.c\", where test.c is the source code you need to print! \ n"); A                 return(1);//Thinking of return (1) What is the difference between return 0 and what is the meaning of their realization? Use return (1) Here to implement a program execution if statement without reporting a segment error at         } -  -         /*Open code file*/ -fp = fopen (argv[1],"R"); -  -         /*Read source code*/ in         intline =1; -  to         /*think about why the middle parameter in Fgets is 256, and if you use other values, what does it really mean? */ +          while(Fgets (Buffer, the, fp)! =NULL) -         { the                 /*Debug to print*/ *                 //puts ("1"); $fprintf (stdout,"%4d:%s", line++, buffer);Panax Notoginseng         } -  the fclose (FP); +  A         return 0; the}

The results of the implementation are as follows:

By writing a C language program, the runtime implementation prints the source code and line number of another program

Related Article

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.