About conversion of os.path.join in C++

Chen0729: <p>Hi, All</p>
<p>I have inquiry about how to realize os.path.join in C++ In libtorch</p>
<p>In python, it is<br>
Torch::load(os.path.join(arg.savedir,‘d.pkl’)</p>
<p>Many thanks.</p>

ptrblck: <p><code>os</code> is a Python lib and thus not available in C++.<br>
You could use e.g. <code>std::filesystem</code> to use a similar as <code>pathlib</code>:</p>
<pre><code class="lang-auto">#include &lt;iostream&gt;
#include &lt;filesystem&gt;
namespace fs = std::filesystem;

int main() 
{
    fs::path dir ("/tmp");
    fs::path file ("foo.txt");
    fs::path full_path = dir / file;
    std::cout &lt;&lt; full_path &lt;&lt; std::endl;
    return 0;
}
</code></pre>
<p>Code taken from <a href="https://stackoverflow.com/questions/6297738/how-to-build-a-full-path-string-safely-from-separate-strings">here</a>.</p>

Chen0729: <p>Thank you very much. I appreciate that.</p>

Chen0729: <p>One more inquiry is that if pytorch<br>
Is model=torch.load(fullpath)</p>
<p>In c++, shall I use torch::load(model,full path) as I cannot use model=torch::load(full path)</p>
<p>Also how shall I convert</p>
<p>Os.makedir(path) to c++</p>
<p>Thanks a lot.</p>

ptrblck: <p><code>auto module = torch::jit::load(path);</code> might work.</p>
<p><code>mkdir</code> is also a C++ function as described <a href="https://www.geeksforgeeks.org/create-directoryfolder-cc-program/">here</a>.</p>

Chen0729: <p>Thanks a lot. That is very helpful.</p>

Chen0729: <p>But actually it says "no matching function for ‘mkdir’.</p>
<p>What shall I include in the header file in order to find mkdir function please ?</p>
<p>Thanks</p>

ptrblck: <p>The link gives you full examples for Windows and Linux systems.<br>
On Linux <code>mkdir</code> should be defined in <code>&lt;sys/stat.h&gt;</code>.</p>

Chen0729: <p>Thanks.</p>
<p>It seems that even if I include &lt;sys/stat.h&gt;, it stills says no matching function call to “mkdir”. I am in linux, in libtorch</p>

matovitch: <aside class="quote no-group" data-username="Chen0729" data-post="9" data-topic="100401">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chen0729/48/15587_2.png" class="avatar"> Chen0729:</div>
<blockquote>
<p>It seems that even if I include &lt;sys/stat.h&gt;, it stills says no matching function call to “mkdir”. I am in linux, in libtorch</p>
</blockquote>
</aside>
<p>This is the right header though. See an example here:<br>
<a href="https://coliru.stacked-crooked.com/a/ace3992a9a0d474e" class="onebox" target="_blank" rel="noopener nofollow ugc">https://coliru.stacked-crooked.com/a/ace3992a9a0d474e</a><br>
The related man page:</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://linux.die.net/man/3/mkdir">
  <header class="source">
      <img src="https://linux.die.net/favicon.ico" class="site-icon" width="" height="">

      <a href="https://linux.die.net/man/3/mkdir" target="_blank" rel="noopener nofollow ugc">linux.die.net</a>
  </header>

  <article class="onebox-body">
    

<h3><a href="https://linux.die.net/man/3/mkdir" target="_blank" rel="noopener nofollow ugc">mkdir(3): make directory - Linux man page</a></h3>

  <p>The mkdir() function shall create a new directory with name path. The file permission bits of the new directory shall be initialized from mode. These file ...</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>