C ++从txt文件中读取并保存所有内容" |"在一个缓冲区

时间:2014-06-02 11:27:34

标签: c++

嗯,很难准确地解释我想要的东西,我只是举个例子。

{accbeingused0or1}|{accName}|{accPassword}|{gameName}|{gamePassword}

所以它看起来像这样:

0|account123|password123|gamename123|password123

1 个答案:

答案 0 :(得分:3)

你走了:

template< char Ch >
std::istream& Char( std::istream& is )
{
    if( (is >> std::ws).get() != Ch )
        is.setstate( std::ios::failbit );

    return is;
}

struct Data
{
    int acc_used;
    std::string accountName;
    std::string accountPassword;
    std::string gameName;
    std::string gamePassword;
};

std::istream& operator>>( std::istream& is, Data& d )
{
    is >> d.acc_used >> Char<'|'> >> std::ws;
    std::getline( is, d.accountName, '|' );    
    std::getline( is, d.accountPassword, '|' ); 
    std::getline( is, d.gameName, '|' ); 
    is >> d.gamePassword;
    return is;
}