Home >php教程 >PHP源码 >C++ reads content by word from file

C++ reads content by word from file

大家讲道理
大家讲道理Original
2016-11-11 13:38:001893browse

Reading method: Read word by word, use spaces to separate words, stop reading when encountering ".", and the returned content is stored in 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);
    }
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn