{"id":3627,"date":"2018-07-27T15:09:44","date_gmt":"2018-07-27T14:09:44","guid":{"rendered":"http:\/\/roboblog.fatal-fury.de\/?p=3627"},"modified":"2018-07-27T15:09:44","modified_gmt":"2018-07-27T14:09:44","slug":"c-guns-use-fortran-debug-techniques-in-c","status":"publish","type":"post","link":"http:\/\/roboblog.fatal-fury.de\/?p=3627","title":{"rendered":"C++ Guns: use FORTRAN debug techniques in C++"},"content":{"rendered":"<p>On of the first things one learn about FORTRAN: you can very easy write stuff to files without getting worried about file creation of filenames.<br \/>\nJust write to a random number, the file handle. You can do this everywhere in the code. The output is append to the file. And the file gets automatic cleaned on program start.<br \/>\nHere is one example:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nprogram debug\r\n  integer :: x\r\n  x = 1\r\n  write(1001,*) &quot;This is string&quot;, x\r\n  write(1001,*) &quot;The End&quot;\r\nend program\r\n<\/pre>\n<blockquote><p>$ gfortran debug.f90<br \/>\n$ .\/a.out<br \/>\n$ cat fort.1001<br \/>\n This is string           1<br \/>\n The End<\/p><\/blockquote>\n<p>As you can see, the file handle number get prefixed with \"fort.\". This is pretty nice for some nasty debug session, so lets do this with C++!<\/p>\n<p>The implementation in C++ is surprisingly easy, just a single line of code and we are done:<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n#include &lt;fstream&gt;\r\n\r\ntemplate&lt;int fhdl&gt;\r\ninline std::ofstream&amp; write() {\r\n    static std::ofstream ostrm(&quot;cpp.&quot;+std::to_string(fhdl), std::ios::out);\r\n    return ostrm;\r\n}\r\n\r\nint main() {\r\n    int x = 1;\r\n    write&lt;1001&gt;() &lt;&lt; &quot;This is string &quot; &lt;&lt; x &lt;&lt; &quot;\\n&quot;;\r\n    write&lt;1001&gt;() &lt;&lt; &quot;The End\\n&quot;;\r\n}\r\n<\/pre>\n<blockquote><p>$ g++ debug.cpp<br \/>\n$ .\/a.out<br \/>\n$ cat cpp.1001<br \/>\nThis is string 1<br \/>\nThe End<\/p><\/blockquote>\n<p>The file handle number is passed as template parameter. I guess it is always know at compile time during the debug session. Then the <em>write()<\/em> function simply returns a reference to a static standard ofstream object. Which is created during the first call to <em>write()<\/em>. The created files are prefixed with \"cpp.\" And you can use this with any custom type with overloaded operator<<.\n\nThats all.\n\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On of the first things one learn about FORTRAN: you can very easy write stuff to files without getting worried about file creation of filenames. Just write to a random number, the file handle. You can do this everywhere in the code. The output is append to the file. And the file gets automatic cleaned [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[17,30],"class_list":["post-3627","post","type-post","status-publish","format-standard","hentry","category-allgemein","tag-cpp","tag-fortran"],"_links":{"self":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3627","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3627"}],"version-history":[{"count":4,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3627\/revisions"}],"predecessor-version":[{"id":3631,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=\/wp\/v2\/posts\/3627\/revisions\/3631"}],"wp:attachment":[{"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3627"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/roboblog.fatal-fury.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}