標籤

C++ (12) Linux (6) MacOSX (4) Makefile (3) Matlab (3) Ubuntu (3) Android (2) C (1) Refactoring (1)

2011年10月13日 星期四

[C++] 用fstream , string , substr, find來讀檔

每次遇到要用fstream讀檔案時
都覺得很麻煩
尤其是string的find和substr
每次Google每次忘記
所以這次要把它記下來!!

fstream query_file;
query_file.open("OOVquery.syl.txt", fstream::in);

while(!query_file.eof()) {

    char buffer[256];
    query_file.getline(buffer, 256);
    string eachLine = string(buffer);
    text.append(" ");

    int pos = 0;
    int pos2 = 0;

    while((pos2 = text.find(" ", pos)) != string::npos) {
        string subsyl = text.substr(pos, pos2 - pos);

        //這裡記得要+1,不然會一直找到第一個" "
        pos = pos2+1;         
    }
}

沒有留言:

張貼留言