>  기사  >  php教程  >  C++는 파일에서 단어 단위로 내용을 읽습니다.

C++는 파일에서 단어 단위로 내용을 읽습니다.

大家讲道理
大家讲道理원래의
2016-11-11 13:38:001827검색

읽기 방법: 단어 단위로 읽고, 단어를 공백으로 구분하고, "."가 나타나면 읽기를 중지하고, 반환된 내용을 벡터에 저장합니다

//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으로 문의하세요.