国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

用line_as_stream 簡化流的讀取

2019-11-17 05:59:37
字體:
來源:轉載
供稿:網友

 


在將數(shù)據(jù)持久化到文件時,你可能會發(fā)現(xiàn)很難強制要求系統(tǒng)將特定的部分數(shù)據(jù)寫到一行中。將特定的數(shù)據(jù)寫到同一行有時是很有用的,比如在你從流(如一個文件)中讀取一個數(shù)組的時候。

假設你要讀取一個數(shù)組的元素,其中有一行被破壞了(比如丟失了一些數(shù)據(jù))。一般情況下,這會導致后面所有的元素都受損。

作為一個例子,假設我們有一個數(shù)據(jù)結構,是一個窗口數(shù)組,你希望把它持久化到一個文件中,象下面這樣:

第一行:窗口的數(shù)量
后面的每一行都包含兩個值:窗口的寬度和窗口的高度
寫成代碼似乎很簡單:

#include
#include
#include

struct Window
{
Window( int nLength = 0, int nHeight = 0)
: m_nWindowLength( nLength), m_nWindowHeight( nHeight)
{}
int m_nWindowLength;
int m_nWindowHeight;
};

std::ostream & Operator << ( std::ostream & streamOut, const Window & value)
{
streamOut << value.m_nWindowLength << " " << value.m_nWindowHeight;
return streamOut;
}
std::istream & operator >> ( std::istream & streamIn, Window & value)
{
streamIn >> value.m_nWindowLength >> value.m_nWindowHeight;
return streamIn;
}

void write_windows( std::vector< Window> &aWindows, const char * strFileName)
{
std::ofstream streamOut( strFileName);
// 第一行
streamOut << aWindows.size() << std::endl;
// 其余行
std::vector< Window>::iterator itFirst = aWindows.begin(), itLast = aWindows.end();
while ( itFirst != itLast)
{
// 每個窗口的數(shù)據(jù)都在它自己那一行
streamOut << *itFirst << std::endl;
++itFirst;
}
}
但是,要正確地讀出這些數(shù)據(jù),可能會有一些問題:

//可能出錯!!!
void read_windows( std::vector< Window> &aWindows, const char * strFileName)
{
aWindows.clear();
std::ifstream streamIn( strFileName);
int nSize;
streamIn >> nSize;
for ( int idx = 0; idx < nSize; ++idx)
{
Window w;
streamIn >> w;
aWindows.push_back( w);
}
}

上面的代碼并沒有強制任何東西。所有數(shù)據(jù)都被放到一行中,這看起來沒有什么問題。但假如用戶不小心,修改了你的文件,插入了一個多余的值或刪掉了一個值,那么后面所有的元素都會得到錯誤的值,而你的程序并不會意識到這一點。嘗試運行一下下面的代碼并仔細看看其中的注釋:

#include
#include
int main(int argc, char* argv[])
{
std::vector< Window> aWindows;
aWindows.push_back( Window( 100, 400));
aWindows.push_back( Window( 200, 400));
aWindows.push_back( Window( 400, 400));
aWindows.push_back( Window( 500, 500));
aWindows.push_back( Window( 600, 200));
aWindows.push_back( Window( 600, 400));
aWindows.push_back( Window( 600, 690));
write_windows( aWindows, "persist.txt");
std::vector< Window> aReadWindows;
/* 在這里加一個調試斷點;
修改persist.txt,刪除第4行的第一個值*/
read_windows( aReadWindows, "persist.txt");
std::copy( aReadWindows.begin(), aReadWindows.end(),
std::ostream_iterator< Window>( std::cout, "/n"));
/*在這里加一個調試斷點:看看你讀了多少個錯誤的值! */
return 0;
}
還好,你可以用來line_as_stream讀取一行,然后將它看作一個流。用這種方法,你可以確定每個元素是從一行中讀取的。于是,read_windows函數(shù)變成這樣:

void read_windows( std::vector< Window> &aWindows, const char * strFileName)
{
aWindows.clear();
std::ifstream streamIn( strFileName);
int nSize;
// 第一行
line_as_stream( streamIn) >> nSize;
for ( int idx = 0; idx < nSize; ++idx)
{
Window w;
//每個窗口的數(shù)據(jù)都在它自己那一行
line_as_stream( streamIn) >> w;
aWindows.push_back( w);
}
}

現(xiàn)在,重新運行前面的例子,你可以看到只有一個元素受損,如你所料。

這就是line_as_stream的源碼

#include
#include
#include

namespace PRivate
{
template< class char_type, class char_traits>
struct line_stream_holder
{
typedef line_stream_holder< char_type, char_traits> this_class;
typedef std::basic_istringstream< char_type, char_traits> stream_type;
typedef std::basic_string< char_type, char_traits> string_type;
line_stream_holder( const string_type & value)
: m_stream( value)
{}
line_stream_holder( const this_class & source)
: m_stream( source.m_stream.str() )
{}

// allow passing this stream in functions that
// accept streams
operator stream_type & () const
{ return m_stream; }
private:
mutable stream_type m_stream;
};

template< class char_type, class char_traits, class value_type>
inline typename line_stream_holder< char_type, char_traits>::stream_type & operator >> (const line_stream_holder< char_type, char_traits> & streamIn, value_type & value)
{
typedef typename line_stream_holder< char_type, char_traits>::stream_type stream_type;
stream_type & underlyingStream = streamIn;
underlyingStream >> value;
return underlyingStream;
}

} // namespace Private

template< class char_type, class char_traits>
Private::line_stream_holder< char_type, char_traits> line_as_stream(
std::basic_istream< char_type, char_traits> & streamIn, char_type chDelim = '/n')
{
std::basic_string< char_type, char_traits> strLine;
std::getline( streamIn, strLine, chDelim);
return strLine;
}



發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 巴中市| 龙胜| 东丰县| 泸州市| 广平县| 政和县| 安国市| 杂多县| 定陶县| 佳木斯市| 博爱县| 罗甸县| 藁城市| 平顺县| 涞源县| 邯郸县| 漾濞| 潢川县| 临安市| 潍坊市| 西藏| 屯昌县| 延边| 诏安县| 长武县| 汾阳市| 尚志市| 宿州市| 平舆县| 沁源县| 攀枝花市| 阿拉善右旗| 棋牌| 香格里拉县| 彭阳县| 全州县| 永昌县| 桦川县| 胶南市| 五华县| 五指山市|