File Basic Operation---Applet---display the contents of the target file

Source: Internet
Author: User

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

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.