Boost C++ Libraries: Ticket #3323: file_descriptor_sink sink("abc", BOOST_IOS::trunc) not trunc https://svn.boost.org/trac10/ticket/3323 <p> boost 1.39.0 iostreams, on Windows Vista sp2: file_descriptor_sink sink("abc", BOOST_IOS::trunc) not trunc, </p> <p> and file_descriptor_sink sink("abc", BOOST_IOS::app) only need call <a class="missing wiki">SetFilePointer</a>(pimpl_-&gt;handle_, 0, NULL, FILE_END) once, but now every write() call it once, why? if write only, can we call <a class="missing wiki">SetFilePointer</a>() in open() only once, and not set the flag impl::append? </p> <p> void file_descriptor::open </p> <blockquote> <p> ( const std::string&amp; path, BOOST_IOS::openmode m, </p> <blockquote> <p> BOOST_IOS::openmode base ) </p> </blockquote> </blockquote> <p> { </p> <blockquote> <p> using namespace std; m |= base; </p> </blockquote> <p> #ifdef BOOST_IOSTREAMS_WINDOWS <em>---------------------------------------------</em> </p> <blockquote> <p> DWORD dwDesiredAccess; DWORD dwCreationDisposition; if ( (m &amp; (BOOST_IOS::in | BOOST_IOS::out)) </p> <blockquote> <p> == </p> </blockquote> <p> (BOOST_IOS::in | BOOST_IOS::out) ) </p> </blockquote> <blockquote> <p> { </p> <blockquote> <p> if (m &amp; BOOST_IOS::app) </p> <blockquote> <p> throw BOOST_IOSTREAMS_FAILURE("bad open mode"); </p> </blockquote> <p> dwDesiredAccess = GENERIC_READ | GENERIC_WRITE; dwCreationDisposition = </p> <blockquote> <p> (m &amp; BOOST_IOS::trunc) ? </p> <blockquote> <p> OPEN_ALWAYS : OPEN_EXISTING; </p> </blockquote> </blockquote> </blockquote> <p> } else if (m &amp; BOOST_IOS::in) { </p> <blockquote> <p> if (m &amp; (BOOST_IOS::app |BOOST_IOS::trunc)) </p> <blockquote> <p> throw BOOST_IOSTREAMS_FAILURE("bad open mode"); </p> </blockquote> <p> dwDesiredAccess = GENERIC_READ; dwCreationDisposition = OPEN_EXISTING; </p> </blockquote> <p> } else if (m &amp; BOOST_IOS::out) { </p> <blockquote> <p> dwDesiredAccess = GENERIC_WRITE; dwCreationDisposition = OPEN_ALWAYS; if (m &amp; BOOST_IOS::app) </p> <blockquote> <p> pimpl_-&gt;flags_ |= impl::append; </p> </blockquote> </blockquote> <p> } else { </p> <blockquote> <p> throw BOOST_IOSTREAMS_FAILURE("bad open mode"); </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> HANDLE handle = </p> <blockquote> <p> ::CreateFileA( path.c_str(), </p> <blockquote> <p> dwDesiredAccess, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, <em> lpSecurityAttributes dwCreationDisposition, FILE_ATTRIBUTE_NORMAL, NULL ); </em> hTemplateFile </p> </blockquote> </blockquote> <p> if (handle != INVALID_HANDLE_VALUE) { </p> <blockquote> <p> pimpl_-&gt;handle_ = handle; pimpl_-&gt;flags_ |= impl::close_on_exit; </p> </blockquote> <p> } else { </p> <blockquote> <p> pimpl_-&gt;flags_ = 0; throw BOOST_IOSTREAMS_FAILURE("bad open"); </p> </blockquote> <p> } </p> </blockquote> <p> .................. </p> <p> std::streamsize file_descriptor::write(const char_type* s, std::streamsize n) { #ifdef BOOST_IOSTREAMS_WINDOWS </p> <blockquote> <p> if (pimpl_-&gt;flags_ &amp; impl::append) { </p> <blockquote> <p> DWORD const dwResult = </p> <blockquote> <p> ::<a class="missing wiki">SetFilePointer</a>(pimpl_-&gt;handle_, 0, NULL, FILE_END); </p> </blockquote> <p> if ( dwResult == INVALID_SET_FILE_POINTER &amp;&amp; </p> <blockquote> <blockquote> <p> ::<a class="missing wiki">GetLastError</a>() != NO_ERROR ) </p> </blockquote> </blockquote> <p> { </p> <blockquote> <p> throw detail::bad_seek(); </p> </blockquote> <p> } </p> </blockquote> <p> } DWORD ignore; if (!::<a class="missing wiki">WriteFile</a>(pimpl_-&gt;handle_, s, n, &amp;ignore, NULL)) </p> <blockquote> <p> throw detail::bad_write(); </p> </blockquote> <p> return n; </p> </blockquote> <p> ............... </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3323 Trac 1.4.3 Steven Watanabe Mon, 14 Jun 2010 15:56:02 GMT <link>https://svn.boost.org/trac10/ticket/3323#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3323#comment:1</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/62934" title="Rejigger file_descriptors handling of std::ios_base::openmode to match ...">[62934]</a>) Rejigger file_descriptors handling of std::ios_base::openmode to match std::fstream. In particular, truncate existing files, if std::ios_base::trunc is passed. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3323" title="#3323: Bugs: file_descriptor_sink sink(&#34;abc&#34;, BOOST_IOS::trunc) not trunc (closed: fixed)">#3323</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Mon, 14 Jun 2010 16:09:48 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3323#comment:2 https://svn.boost.org/trac10/ticket/3323#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/62935" title="Open files in append mode on Windows instead of seeking to the end at ...">[62935]</a>) Open files in append mode on Windows instead of seeking to the end at every write when std::ios_base::app is passed. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/3323" title="#3323: Bugs: file_descriptor_sink sink(&#34;abc&#34;, BOOST_IOS::trunc) not trunc (closed: fixed)">#3323</a>. </p> Ticket