首頁 >php教程 >PHP源码 >C++從檔案中依照單字讀取內容

C++從檔案中依照單字讀取內容

大家讲道理
大家讲道理原創
2016-11-11 13:38:001899瀏覽

讀取方式: 逐詞讀取, 詞之間用空格區分,遇到"."就停止讀取,返回的內容訪問在vector

//read data from the file, Word By Word
//when used in this manner, we'll get space-delimited bits of text from the file
//but all of the whitespace that separated words (including newlines) was lost.
//http://www.sharejs.com
vector<string> ReadDataFromFileWBW()
{
    ifstream fin("Input.txt"); 
    string strWord("");
    vector<string> v;
    while( fin >> strWord )
    {
    //字符串string类的比较要用compare函数
        if (strWord.compare(strWord.length()-1, 1, ".") == 0)  
        {
            strWord.erase(strWord.length()-1, 1);
            v.push_back(strWord);
            return v;
        }  
        v.push_back(strWord);
    }
}
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn