CODES:
1# include<stdio.h>2# include<stdlib.h>3 intMain (intargcChar*argv[])4 {5 6 intch; 7FILE *FP;//Create a file pointer8 //=============================================================9 //Exception HandlingTen if(ARGC! =2)//if the receiving parameter is wrong, the output corresponding error message reminds the user of the idiot:) One { Aprintf"usage:%s filename", argv[0]);//use: Open file name with program name -Exit1); - } the if((Fp=fopen (argv[1],"R")) = = NULL)//If you fail to open the target file, the output corresponding error message alerts the user:) - - { -printf"open \ "%s\" Failed!", argv[1]);//failed to open filename +Exit1); - } + //=================================================================== A at - -ch = getc (FP);//reads a character from the specified file one character read - - while(ch! = EOF)//The read character is not a terminator, the contents of the file are not read, and the contents of the file are read after reading - { inPutchar (CH);//output read characters to console -ch = getc (FP);//Loop reads the assigned value of the word from the specified file into ch to } + fclose (FP); - return 0; the}
View Code
Argv[0] Indicates the file name of the program
ARGV[1] indicates that the first parameter accepted is the file name to be manipulated in this program
Getc (file *fp) reads a character to a file specified by the specified pointer
If you read the file characters and find that you have reached the end of the file content, the GETC () function returns a special value of--eof.
The C program will only find the end of the file until the end of the file is read.
File Basic Operation---Applet---display the contents of the target file