Check that file exists: Difference between revisions
Content added Content deleted
(Removed post, for the time being, because it requires more work to fully complete the task.) |
(New post using C++17 syntax. An existing post using an external library, boost, was retained.) |
||
Line 965: | Line 965: | ||
testfile("/docs"); |
testfile("/docs"); |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
===Using C++ 17=== |
|||
C++ 17 contains a Filesystem library which significantly improves manipulations with files. |
|||
<syntaxhighlight lang="c++"> |
|||
#include <iostream> |
|||
#include <filesystem> |
|||
void file_exists(const std::filesystem::path& path) { |
|||
std::cout << path; |
|||
if ( std::filesystem::exists(path) ) { |
|||
if ( std::filesystem::is_directory(path) ) { |
|||
std::cout << " is a directory" << std::endl; |
|||
} else { |
|||
std::cout << " exists with a file size of " << std::filesystem::file_size(path) << " bytes." << std::endl; |
|||
} |
|||
} else { |
|||
std::cout << " does not exist" << std::endl; |
|||
} |
|||
} |
|||
int main() { |
|||
file_exists("C:/input.txt"); |
|||
file_exists("C:/zero_length.txt"); |
|||
file_exists("C:/docs/input2.txt"); |
|||
file_exists("C:/docs/zero_length2.txt"); |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
</pre> |
|||
"C:/input.txt" exists with a file size of 11 bytes. |
|||
"C:/zero_length.txt" exists with a file size of 0 bytes. |
|||
"C:/docs/input2.txt" exists with a file size of 11 bytes. |
|||
"C:/docs/zero_length2.txt" exists with a file size of 0 bytes. |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |