Learn C language to do exercises? The number of characters in the statistics file

Source: Internet
Author: User

Number of characters in the statistics file (with command line arguments)

#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 is:%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;
}

Number of characters in the statistics file (without command line arguments)

#include <stdio.h>
#include <stdlib.h>
#define MAX 80
int main (void)
{
FILE *FP;
Char ch;
Char Name[max];
Long count=0;

printf ("Please 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;
}

Learn C language to do exercises? The number of characters in the statistics 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.