본문 바로가기

C++

[C++] 실습 텍스트 파일 파싱하기







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
 
#include <string>
#include <cstring>
#include <fstream>
 
using namespace std;
 
 
 
 
int main()
 
{
 
    // 파일 읽기 준비
 
    ifstream in("test.txt");
 
    string s="";
    string goal_start="<span class=\"ah_k\">";
    string goal_end="</span>";
    string end_of_file="</body></html>";
 
    string * rank=new string[50];
    int count=0;
    
    cout<<"Naver Searching Rank"<<endl;
    
    while(s!=end_of_file)
    {
        getline(in, s);
        if(!s.find(goal_start))    //0은 거짓 0이 아니면 참 
        {
            s.erase(0,goal_start.length());
            s.erase(s.length()-goal_end.length(), s.length());
            rank[count]=s;
            cout<<count<<"th : "<<rank[count]<<endl;
            count++;
        }
        
        
    }
 
    return 0;
 
}
cs