繁体   English   中英

Getline 采用整个文本而不是一行

[英]Getline takes the whole text instead of one line

我有一个问题,当我尝试 getline 时,不是扫描一行程序而是选择获取整个文本。 我尝试添加 delim 也为此包含字符串,但它似乎仍然不起作用。 向量只是一个大字符串。

void convert_wishlist(ifstream& file, vector<string>& wishlist, int& budget){
    assert(file.is_open());
    string g;
    file>>budget;
    while (!file.fail()) {
        getline(file, g);
        wishlist.push_back(g);
    }
}

前段时间,我不得不编写代码准备处理来自 Windows 或 Linux 的文本文件,所以我编写了自己的 getline 版本,为这两种文件都做好了准备。 这对我有用

template<class CT, class TT, class AT> 
inline
std::basic_istream<CT, TT>& getline(
    std::basic_istream<CT, TT>&    instr,
    std::basic_string<CT, TT, AT>& str)
{
    using is_type                   = std::basic_istream<CT, TT>;
    using sentry_type               = typename is_type::sentry;
    std::ios_base::iostate state    = std::ios_base::goodbit;
    auto changed                    = false;
    const sentry_type sentry(instr, true);

    if (sentry)
    {   
        // State okay, extract characters

        try
        {
            auto sb = instr.rdbuf();
            str.erase();
            const auto delimNL = TT::to_int_type('\n');
            const auto delimCR = TT::to_int_type('\r');
            auto ch            = sb->sgetc();

            for (; ; ch = sb->snextc())
            {
                if (TT::eq_int_type(TT::eof(), ch))
                {   
                    // End of file, quit
                    state |= std::ios::eofbit;
                    break;
                }
                else if (TT::eq_int_type(ch, delimNL))    
                {
                    // Got a newline, discard it and quit.

                    changed = true; // We did read something successfully
                    sb->sbumpc();   // Discard this character
                    break;          // Quit
                }
                else if (TT::eq_int_type(ch, delimCR))
                {   
                    // Got a carriage return.  

                    changed = true; // We did read something successfully
                    sb->sbumpc();   // Discard this character

                    // Next character might be a newline. If so, do the
                    // same for that

                    if (TT::eq_int_type(sb->sgetc(), delimNL))
                        sb->sbumpc();

                    break;          // We are done
                }
                else if(str.max_size() <= str.size())
                {   
                    // String too large, quit

                    state |= std::ios_base::failbit;
                    break;
                }
                else
                {   
                    // Got a character, add it to string

                    str += TT::to_char_type(ch);
                    changed = true;
                }
            }
        }
        catch(...)
        {
            // Some exception.  Set badbit and rethrow

            instr.setstate(std::ios_base::badbit);  
            throw;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM