Boost C++ Libraries: Ticket #4824: Boost file system function file_size() function is returning 0 for /proc virtual file system files. https://svn.boost.org/trac10/ticket/4824 <p> The tellg function of ifstream object is returning -1 when the position of file pointer is queried for /proc virtual file system files. Below is the example program to demonstrate the problem </p> <p> #include &lt;iostream #include &lt;fstream&gt; using namespace std; </p> <p> int main( int argc, char* argv[] ){ </p> <blockquote> <p> long begin,end; ifstream myfile (argv<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a>); begin = myfile.tellg(); cout &lt;&lt; " begin " &lt;&lt; begin &lt;&lt; endl; myfile.seekg (0, ios::end); end = myfile.tellg(); myfile.close(); cout &lt;&lt; "size is: " &lt;&lt; (end-begin) &lt;&lt; " bytes.\n"; return 0; </p> </blockquote> <p> } </p> <p> pmanth2@cyder:~$ ./fstream partA.cpp ( Normal text file ) </p> <blockquote> <p> begin 0 </p> </blockquote> <p> size is: 6692 bytes. </p> <p> pmanth2@cyder:~$ ./fstream /proc/cpuinfo ( virtual file system file ) </p> <blockquote> <p> begin 0 </p> </blockquote> <p> size is: -1 bytes. </p> <p> The tellg() should return the file pointer location for /proc file system files also. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4824 Trac 1.4.3 anonymous Wed, 10 Nov 2010 15:55:31 GMT summary changed https://svn.boost.org/trac10/ticket/4824#comment:1 https://svn.boost.org/trac10/ticket/4824#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">tellg () function of ifstream object is returning -1 for /proc virtual file system files.</span> → <span class="trac-field-new">Boost file system function file_size() function is returning 0 for /proc virtual file system files.</span> </li> </ul> <p> The boost filesystem function file_size returning 0 for /proc file system files. Below is the example program to demonstrate the problem. </p> <p> #include &lt;boost/filesystem/operations.hpp&gt; #include &lt;iostream&gt; </p> <p> namespace fs = boost::filesystem; </p> <p> int main( int argc, char* argv[] ) { </p> <blockquote> <p> if ( argc != 2 ) { </p> <blockquote> <p> std::cout &lt;&lt; "Usage: file_size path\n"; return 1; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> fs::path p( argv<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a>, fs::native ); </p> </blockquote> <blockquote> <p> if ( !fs::exists( p ) ) { </p> <blockquote> <p> std::cout &lt;&lt; "not found: " &lt;&lt; argv<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a> &lt;&lt; std::endl; return 1; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> if ( !fs::is_regular( p ) ) { </p> <blockquote> <p> std::cout &lt;&lt; "not a regular file: " &lt;&lt; argv<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a> &lt;&lt; std::endl; return 1; </p> </blockquote> <p> } </p> </blockquote> <blockquote> <p> std::cout &lt;&lt; "size of " &lt;&lt; argv<a class="changeset" href="https://svn.boost.org/trac10/changeset/1" title="Import core sources for SVNmanger 0.38 ">[1]</a> &lt;&lt; " is " &lt;&lt; fs::file_size( p ) ---------- boost file system file_size function </p> <blockquote> <p> &lt;&lt; std::endl; </p> </blockquote> <p> return 0; </p> </blockquote> <p> } </p> <p> Output: </p> <p> pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ./new k size of k is 46 pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc -c k 46 k pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc -c /proc/cpuinfo 10102 /proc/cpuinfo pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ./new /proc/cpuinfo size of /proc/cpuinfo is 0 --------------------- ( should return actual size of file just as word count ) </p> Ticket Jeremiah Willcock Wed, 10 Nov 2010 16:03:39 GMT <link>https://svn.boost.org/trac10/ticket/4824#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4824#comment:2</guid> <description> <p> Are you sure that these are not just the values returned by the kernel? <code>ls -l</code> on <code>/proc/cpuinfo</code> returns a size of 0 as well. I do not know about the <code>tellg</code> behavior, but it could be from a similar issue. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Wed, 10 Nov 2010 17:49:33 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4824#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4824#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/4824#comment:2" title="Comment 2">jewillco</a>: </p> <blockquote class="citation"> <p> Are you sure that these are not just the values returned by the kernel? <code>ls -l</code> on <code>/proc/cpuinfo</code> returns a size of 0 as well. I do not know about the <code>tellg</code> behavior, but it could be from a similar issue. </p> </blockquote> <p> Hi! </p> <p> Thanks for the quick response. </p> <p> "ls -ltr /proc/cpuinfo" is retruning 0 whereas "wc /proc/cpuinfo" retruning size 10102 </p> <p> pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ ls -ltr /proc/cpuinfo -r--r--r-- 1 root root 0 2010-11-10 10:40 /proc/cpuinfo </p> <p> pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ wc /proc/cpuinfo 312 1872 10102 /proc/cpuinfo </p> <p> pmanth2@cyder:~/new-saga-code/trunk/adaptors/default/file$ </p> <p> I guess file_size should return 10102 in this case.. I think this is special case for proc files. </p> <p> When normal files are considered both "ls -ltr " and wc give the same result. </p> <p> thanks pradeep </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Beman Dawes</dc:creator> <pubDate>Fri, 03 Dec 2010 21:00:22 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4824#comment:4 https://svn.boost.org/trac10/ticket/4824#comment:4 <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">worksforme</span> </li> </ul> <p> On my Linux <a class="missing wiki">VirtualBox</a>: </p> <blockquote> <p> $ ls -l /proc/cpuinfo -r--r--r-- 1 root root 0 2010-12-03 15:43 /proc/cpuinfo </p> </blockquote> <p> so I'm closing this issue as "Works for me". </p> <p> I think you may be confusing the results returned by various usages of the command line interpreter you are using with the results being reported by your operating system's API (which is POSIX-based, in the case of Linux). </p> <p> The Boost filesystem library is just reporting whatever the operating system's API tells it. In the case of file size, the ls -l file size is a pretty good surrogate. The results of wc are a very poor surrogate. </p> <p> "When normal files are considered both "ls -ltr " and wc give the same result." </p> <p> Check you tests again - the two commands give very different results, as expected. </p> <p> HTH, </p> <p> --Beman </p> Ticket