Number and number of characters in the statistics file for C Language exercises

Source: Internet
Author: User

Number and number of characters in the statistics file for C Language exercises
Count the number of characters in the file (using command line parameters)

# Include <stdio. h>
# Include <stdlib. h>
Int main (int argc, char * argv [])
{
Char ch;
FILE * fp;
Long count = 0;

If (argc! = 2)
{
Printf ("File Name: % s \ n", argv [0]);
Exit (EXIT_FAILURE );
}
If (fp = fopen (argv [1], "r +") = NULL)
{
Fprintf (stderr, "cannot open file \" % s \ "\ n", argv [1]);
Exit (EXIT_FAILURE );
}
While (ch = getc (fp ))! = EOF)
{
Putc (ch, stdout );
++ Count;
}
Fclose (fp );
Printf ("File % s has % ld characters \ n", argv [1], count );
Return 0;
}

 

Count the number of characters in the file (command line parameters are not used)

# Include <stdio. h>
# Include <stdlib. h>
# Define MAX 80
Int main (void)
{
FILE * fp;
Char ch;
Char name [MAX];
Long count = 0;

Printf ("Enter the file name :");
Gets (name );
If (fp = fopen (name, "r +") = NULL)
{
Fprintf (stderr, "cannot open file % s \ n", name );
Exit (EXIT_FAILURE );
}
While (ch = getc (fp ))! = EOF)
{
Putc (ch, stdout );
Count ++;
}
Fclose (fp );
Printf ("File % s has % ld characters \ n", name, count );
Return 0;
}

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.