- UID
- 1023
- UCC
-
- 声望
-
- 好评
-
- 贡献
-
- 最后登录
- 1970-1-1
|
- // Nov 14, 2008 - by Euyis
0 V; I9 d3 t1 J# t/ v0 F* k - " u2 I; u, U6 w2 }' w1 k& A
- //. V% a6 s, {6 p1 n# O
- // I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.. T0 ]$ f) ?: h1 K- Q+ b2 _9 U
- // In case this is not legally possible,' R8 W% p o% n2 a) B
- // I grant any entity the right to use this work for any purpose, without any conditions, unless such conditions are required by law.
9 E( }. Z1 b9 _ - //
$ H2 B) Z( c5 [ - 6 z8 x4 e+ z; q! B. E
- #include <iostream>5 Y2 d: O* m5 T4 ~4 b& ]
- #include <fstream>) U. F; s% _0 q& T
- #include <cctype>( K0 c. P1 i' J- Z5 z6 O
- using namespace std;6 f& @1 z: ^! t( y* V1 ]" c" x6 ^5 m
7 y; Y7 q6 o( ]- // MACROS6 D( p3 d p8 L3 b
- #define SYSTEM_BLOCK_STR "[system]"
' r& P8 V8 n; \1 A - #define NICKNAME_STR "nickname = " Y0 N( \& V" i& j% W h
- #define IDS_STR "strid_name = "
$ u1 s1 s$ [( [ - //
9 c8 h( c M( p, t4 N6 T X
4 ?, k+ N" x* r% N$ q9 @/ ^0 H- ifstream inf;
2 _. v# L6 @2 [( O - char *buffer;
# q5 J0 Q' c) k - ) G6 Q% n4 T. ?* E# {
- int main(int argc, char *argv[])
2 y: ]9 J) a1 Z% k8 l0 c6 Z - {7 o: t) d1 @; w
- int i;
* C3 Q0 G# v0 O# q; | - ; }0 L5 `1 w6 h v% {- E5 o( a' g
- if(argc!=2){ - T( C8 X. t! b" r2 n. O4 p
- cout << "This program requires one and only one argument.";
) f$ N3 ~! T6 |( N9 v - return -1;
- i1 C; z2 ? o: p - }
1 ?6 `9 K) M- U; Y* {$ ] -
% q$ O8 `2 _7 G0 k4 r! E5 v# g+ ?3 K - inf.open(argv[1], ios_base::in | ios_base::ate);4 @' F6 U; F5 M# ]
- if(!inf) {
; O K1 ~- r5 ?" s: P9 G - cout << "Error opening file.";
7 X+ }: O* K d5 @9 g& v - return -1;0 C" N3 U) V: [$ c
- }+ q8 O. P5 O# A) Q8 ~4 Y {, q
- + m3 Z6 ]9 A0 k* I6 h! \; s
- int streamLength = inf.tellg();
/ Y! Z$ }7 v0 W; V - if(streamLength!=-1) {
# W) {0 f! { M9 `$ W" _ - inf.seekg(0, ios::beg);: _/ |& p% ^+ m
- & f. l' ~) {" m# P3 J
- buffer = new char [streamLength];% S* Y: q+ l$ q8 Z" Y A) ^7 A' ~
- // Read file;+ t( P, ?$ D0 q
- for(i=0;i<streamLength;i++) {; D% V' [, I: s. s
- inf.get(buffer[i]);, U8 D6 ]4 F# N. W. |
- buffer[i] = tolower(buffer[i]);7 r* l% P* L2 d" y) O! G9 N
- }) x: }8 Z; A4 @6 z& `4 b
-
9 |9 m: b( C! |1 M9 j4 T# [7 p - /* For testing
9 h0 D4 z0 ^ B# J+ `* b - for(i=0;i<streamLength;i++) {
" F: { u0 n$ o4 ] - cout << buffer[i];- d1 p; I3 y0 Z3 J
- }
7 U' e. v" y3 Y f3 \; A9 o - */: `, b7 v/ }' F7 K2 v3 W
- }
7 C% C# f: h5 @ - else {) t/ ~+ s \( O! r# J5 u3 a& i
- cout << "Failed to read file." << endl;0 G+ X7 H- U4 P$ d+ T6 W% X$ ~
- return -1;3 J8 g* v8 e% H! J) `3 K) a
- }
5 c1 a% ?6 I; Y3 g3 ~( i -
) B5 F% b1 T3 P1 U - string processStr (buffer);1 w+ B& i1 o4 k3 K& H! S3 K
- delete [] buffer;# E8 }4 y1 @: z+ o& C' g
- / `) s: U6 v6 e2 q: E' Q! X
- // DUMB DUMB DUMB....; X) H. O/ F/ j% y: M$ a+ ?3 @4 W% |! s0 e
-
% V! w/ w* e/ k) ~& o; l - int nextSearchPos = 0;+ w2 O- G9 y3 _) Y, }; E
- int tmpSearchPos[2] = {0};
2 x$ [2 F0 P- K! I0 [7 W+ N - int entriesCount = 0;
3 w+ A! m( z0 Z1 [' o$ j -
- |$ y9 C0 n( W8 F O -
& p i* M" r( y! f* X2 \ - 0 i x' p7 ?! E, @
- while(processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)!=string::npos) {& z" l$ P# b9 s" U
- nextSearchPos = processStr.find(SYSTEM_BLOCK_STR, nextSearchPos)+8; G# R: _; d: e! ]4 B
- tmpSearchPos[0] = processStr.find(NICKNAME_STR, nextSearchPos);
$ ?( }$ I+ e1 L7 [ - tmpSearchPos[1] = processStr.find(IDS_STR, nextSearchPos);6 U' f% y: }- Q4 a
-
+ Z, G9 h/ i4 z& E6 b - if(tmpSearchPos[0]!=string::npos) {
5 o& n5 p5 T0 K7 g$ U - i=tmpSearchPos[0]+11;
) e3 i6 m1 {9 F2 _9 M s - : B1 S- r4 B- G7 j, G8 u f. v
- while(processStr[i]!='\n' && processStr[i]!='\r') {# f$ ]' f' `. n; D
- cout << processStr[i];
9 a) N# B' G9 C) g& `+ Y - i++;4 w0 g% H; Q) x& ~' J( V; d- G2 ?
- }( q6 p. |, c4 T+ ?% c' h
- $ |7 }# [, j4 t* |& q; b w% H8 N
- cout << endl;
! a/ M* d5 _; G8 W8 B1 E( ]2 L; y- b: w - + G- W6 F5 z! a ?! @
- }
& g1 G J' d* l2 a K7 o% [ -
" B' _5 h7 _* g3 }' C+ o - if(tmpSearchPos[1]!=string::npos) {9 S( f( V( m7 R9 K; t
- i=tmpSearchPos[1]+13;2 X- u0 K5 a% e. X4 V
- ) I/ z- q& o' |& I+ {
- while(processStr[i]!='\n' && processStr[i]!='\r') {
9 y8 B. l9 I# \& _$ K3 ^ - cout << processStr[i];- p! t/ R# @" ~
- i++;, z2 J% Q0 t; u8 z! r; U5 q* c
- }
4 P! O3 I6 |: T" I, I+ ? -
" e9 F- j" T6 |# f - cout << endl;
$ O. I3 z- s( X -
3 B' c2 [+ X8 r2 ^) v3 k/ S - }
( j6 f! _/ U) D" `! U% h -
* Y: j J$ b9 N+ {3 `- G - entriesCount++;
1 \+ k+ x% n" T; W - }; u! [4 M7 X, Z2 X, I
- * I4 Q" r1 d0 k
- cout<<entriesCount;
1 O6 u8 T8 P- b+ ^+ g - return 0;
: ~4 H2 _/ y3 V0 b$ k -
! ~9 T' W. `; V1 j4 P; |7 V - }
复制代码 Update: 最后一行输出条目数.
! i' D% h, q/ f9 S4 W4 [
( N7 j- Z5 z5 _6 y4 `9 i4 l[ 本帖最后由 Euyis 于 2008-11-14 22:21 编辑 ] |
|