首页  >  文章  >  php教程  >  C++从文件中按照单词读取内容

C++从文件中按照单词读取内容

大家讲道理
大家讲道理原创
2016-11-11 13:38:001871浏览

读取方式: 逐词读取, 词之间用空格区分,遇到"."就停止读取,返回的内容存取在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