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 this programming cycle is not long, so the specific features are not refined.
#include "stdafx.h" #include "string.h" #include "stdlib.h" void Count (char * file); int Zicount=0;int wordcount=0;int hangcount=0;int main (int argc, char* 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 number printf ("File%s characters:%d\n", Argv[2],zicount); else if (strcmp (argv[1], "-w") ==0)//Statistics file The number of words printf ("file%s Word number:%d\n", Argv[2],wordcount); else if (strcmp (argv[1], "-l") ==0)//Total number of lines of statistics printf ("file%s total number of rows is%d\n", argv[2],hangcount);// else if (strcmp (argv[1], "-a") ==0)//Return 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 } else printf ("The 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; */}void Count (char * file) {file * FP; Char A; if ((Fp=fopen (file, "R")) {==null) {printf ("Read file failed! \ 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 theFile,zicount would add hangcount++; Can ' t recognize the last line fclose (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 must go to contact more things, not sit idle
C-language word-count program for Coding and command execution