1 //++++++++++++++++++++++++++
2 // line_count.cpp
3 //++++++++++++++++++++++++++
4
5 ///#include head file
6 #include <iostream>
7 #include <fstream>
8
9
10 ///USING
11 using namespace std;
12
13
14 ///main_entry
15 main(int argc,char *argv[])
16 {
17
18
19 ///declare
20 ifstream in_file;
21 char ch;
22 int word_count, numlines;
23
24
25 ///Open in_file
26 in_file.open(argv[1]);
27
28
29 ///Is file ?
30 if (in_file==NULL)
31 {
32
33
34 ///Prompt error !
35 cout << "Cannot open" << argv[1] << endl;
36
37
38 ///main_EXIT
39 return(1);
40
41
42 /// +
43 }//end if
44
45
46 ///Initial value
47 word_count=0;
48 numlines = 0;
49
50
51 ///Read a char
52 in_file.get(ch);
53
54
55 ///Loop till EOF
56 while(in_file)
57 {
58
59
60 ///Loop till EOL
61 while(in_file && ch != '\n')
62 {
63
64
65 ///Increase counter
66 word_count++;
67
68
69 ///Read a char
70 in_file.get(ch);
71
72
73 /// +
74 }//end while
75
76
77 ///Increase line no
78 numlines++;
79
80
81 ///Read a char
82 in_file.get(ch);
83
84
85 /// +
86 }//end while
87
88
89 ///Display Line No
90 cout <<argv[1] <<" = "<< numlines << " lines <<\n";
91
92
93 ///Display Char No
94 cout << argv[1]<<" = " << word_count << " chars w/o eol" << endl;
95
96
97 ///main_end
98 return 0;
99 } //main_end
100
101
This Blog is dedicated to Sir Johnson Lin
CPP : Get a file's line number and character number
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言