Code C language Word statistics program executed with the command line

Source: Internet
Author: User
Tags strcmp

Requirements Introduction

The mode in which the program handles user requirements is:

    • Wc.exe [Parameter][filename]

In [parameter], the user interacts with the program through input parameters, the following functions are required:

1. Basic functions

    • Supports the-c statistic file character number
    • Supports-W statistics file Word count
    • Supports-l statistics file Total rows

2, expand the function

    • Support-A returns advanced options (line of code lines with blank lines of comment)
    • Supports-s recursive processing of eligible files

3. Advanced Features

    • Support for the-X program to interact with users in a graphical interface

[FileName] is the file name to be processed.

Design History and Impressions

First see this topic, really is confused, because the previous compiled code is compiled directly with the compiler, and this time asked to write a command-line program, so I found a degree niang, degrees Niang tell me, main function parameters

int argc, char* argv[] can help you, by reading the relevant documents, I have a general understanding of it:

ARGC This shaping data is used to hold the space size of the subsequent string argv, and argv is used to hold the command line input data.

Understand these, the idea immediately opened, this topic requires nothing more than the command line to the main function input control parameters, so as to achieve the effect of the condition count

Basic functions

    • Supports the-c statistic file character number
    • Supports-W statistics file Word count
    • Supports-l statistics file Total rows

Because the timing is not appropriate, resulting in the programming cycle is not long, so the specific features are not refined, the following attached code, the personal feeling the biggest bright spot is the command line execution code, words do not say, paste code, hair, sleep!

#include"stdafx.h"#include"string.h"#include"stdlib.h"voidCount (Char*file); intZicount=0;intWordcount=0;intHangcount=0;intMainintargcChar* argv[])//argv[1] Save instruction, argv[2] Save file path{FILE*FP; Count (argv[2]);  while(1)    {        if((Fp=fopen (argv[2],"R"))==NULL) {printf ("The file does not exist! \n\n\n"); scanf ("%s%s%s", argv[0],argv[1],argv[2]); Continue; }        Else if(strcmp (argv[1],"- C")==0)//Statistics file character Countprintf"the file%s character count is:%d\n", argv[2],zicount); Else if(strcmp (argv[1],"- W")==0)//statistics file number of wordsprintf"file%s Word number is:%d\n", argv[2],wordcount); Else if(strcmp (argv[1],"- L")==0)//total rows of statistical textprintf"file%s Total number of rows is%d\n", argv[2],hangcount);//Else if (strcmp (argv[1], "-a") ==0)//back to Advanced options//printf ("4\n"); //Else if (strcmp (argv[1], "-S") ==0)//recursive processing of eligible files//printf ("5\n");        Else if(strcmp (argv[1],"Exit")==0) {printf ("Program End!\n");  Break; }        Elseprintf ("This directive does not exist, please re-enter \ n"); printf ("\ n"); scanf ("%s%s%s", argv[0],argv[1],argv[2]); }    return 0; /*Char a[20];    strcpy (A, "haha.txt");    Count (a);        printf ("%d\n%d\n%d\n", Zicount,wordcount,hangcount);        return 0; */}voidCount (Char*file) {FILE*FP; CharA; if(Fp=fopen (file,"R"))==NULL) {printf ("failed to read the file! \ n"); Exit (-1); }     while(!feof (FP)) {a=fgetc (FP); if(a!=' '&&a!='\ n'&&a!='\ t'&&a!=','&&a!='.'&&a!='!'&&a!=';'&&a!='=') Zicount++; if(a==' '|| a=='\ n'|| a=='\ t'|| a==','|| a=='.'|| a=='!'|| a=='='|| a==';')        {            if(a=='=')//Resolve = =wordcount--; Wordcount++; }        if(a=='\ n'|| a=='\ t') Hangcount++; } Zicount--;//At end of the File,zicount would addhangcount++;//can ' t recognize the last linefclose (FP);}

Execution effect:

Sentiment article

By writing this command-line program, I realized that the previous contact is too little, trapped in a small circle afraid to go out, after the need to contact more things, must not sit idle.

Code C language Word statistics program executed with the command line

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.