Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Unfortunately the C++ standard does not define a standard way of working with files and folders in this way. </p> <p>Since there is no cross platform way, the best cross platform way is to use a library such as the <a href="http://www.boost.org/doc/libs/1_38_0/libs/filesystem/doc/index.htm" rel="noreferrer">boost filesystem module</a>.</p> <p><strong>Cross platform boost method:</strong></p> <blockquote> <p>The following function, given a directory path and a file name, recursively searches the directory and its sub-directories for the file name, returning a bool, and if successful, the path to the file that was found. </p> </blockquote> <pre><code>bool find_file(const path &amp; dir_path, // in this directory, const std::string &amp; file_name, // search for this name, path &amp; path_found) // placing path here if found { if (!exists(dir_path)) return false; directory_iterator end_itr; // default construction yields past-the-end for (directory_iterator itr(dir_path); itr != end_itr; ++itr) { if (is_directory(itr-&gt;status())) { if (find_file(itr-&gt;path(), file_name, path_found)) return true; } else if (itr-&gt;leaf() == file_name) // see below { path_found = itr-&gt;path(); return true; } } return false; } </code></pre> <p>Source from the boost page mentioned above.</p> <hr> <p><strong>For Unix/Linux based systems:</strong> </p> <p>You can use <a href="http://www.manpagez.com/man/3/opendir/" rel="noreferrer">opendir</a> / <a href="http://www.manpagez.com/man/3/readdir/" rel="noreferrer">readdir</a> / <a href="http://www.manpagez.com/man/3/closedir/" rel="noreferrer">closedir</a>. </p> <blockquote> <p>Sample code which searches a directory for entry ``name'' is:</p> </blockquote> <pre><code> len = strlen(name); dirp = opendir("."); while ((dp = readdir(dirp)) != NULL) if (dp-&gt;d_namlen == len &amp;&amp; !strcmp(dp-&gt;d_name, name)) { (void)closedir(dirp); return FOUND; } (void)closedir(dirp); return NOT_FOUND; </code></pre> <p>Source code from the above man pages.</p> <hr> <p><strong>For a windows based systems:</strong> </p> <p>you can use the Win32 API <a href="http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx" rel="noreferrer">FindFirstFile</a> / <a href="http://msdn.microsoft.com/en-us/library/aa364428(VS.85).aspx" rel="noreferrer">FindNextFile</a> / <a href="http://msdn.microsoft.com/en-us/library/aa364413(VS.85).aspx" rel="noreferrer">FindClose</a> functions.</p> <blockquote> <p>The following C++ example shows you a minimal use of FindFirstFile.</p> </blockquote> <pre><code>#include &lt;windows.h&gt; #include &lt;tchar.h&gt; #include &lt;stdio.h&gt; void _tmain(int argc, TCHAR *argv[]) { WIN32_FIND_DATA FindFileData; HANDLE hFind; if( argc != 2 ) { _tprintf(TEXT("Usage: %s [target_file]\n"), argv[0]); return; } _tprintf (TEXT("Target file is %s\n"), argv[1]); hFind = FindFirstFile(argv[1], &amp;FindFileData); if (hFind == INVALID_HANDLE_VALUE) { printf ("FindFirstFile failed (%d)\n", GetLastError()); return; } else { _tprintf (TEXT("The first file found is %s\n"), FindFileData.cFileName); FindClose(hFind); } } </code></pre> <p>Source code from the above msdn pages.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload