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