If the path (in generic format) does not include root name, returns path().
path() meaning the default constructor for a path object, which is an empty path. So it's conforming to the documentation, since your fullPath variable does not include the root name.
It looks like the tutorial's example of C:/temp uses an absolute path, and therefore it "includes the root name", so it is able to print C: as the root name.
I think the reasoning here is that, given a relative path "foo/bar", it has no idea if you're referring to C:/foo/bar, D:/foo/bar, or some other combination of subfolders.
path() meaning the default constructor for a path object, which is an empty path. So it's conforming to the documentation, since your fullPath variable does not include the root name.
It looks like the tutorial's example of C:/temp uses an absolute path, and therefore it "includes the root name", so it is able to print C: as the root name.
I think the reasoning here is that, given a relative path "foo/bar", it has no idea if you're referring to C:/foo/bar, D:/foo/bar, or some other combination of subfolders.
Thank you for the fast reply! You were correct. Also your explanation makes total sense!
Also I tried using current_path() but had an issue: std::cout << "current_path() = " << fs::current_path() << '\n';
Edit: It mentions the double slash on this page: https://en.cppreference.com/w/cpp/filesystem/path/path but appears to have outdated or wrong code. It shows an example of source code then it's output but when I actually run the code (on their site) it does not give the output they said in the post. You can see the example I'm referring to at the very bottom of the page (of link provided).
#include <iostream>
#include <string>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string version = "1_13";
// Sets file path
fs::path filePath(fs::current_path() /= version);
// Should replace all \'s with / in the path:
for (char c : filePath) {
if (c == "\\") {
c = '/';
}
}
std::cout << filePath;
/*
// ------------- Also, this doesn't work and I can't figure out why. --------------
// Will iterate through every file in the directory of the path stated and print the file names.
for (const auto& entry : fs::directory_iterator(filePath)) {
std::cout << entry.path() << std::endl;
}
*/
return 0;
}
How can I fix this? My end goal is to able to use current_path() /= version to access a directory of files. And I'm using current_path() so if I shared the program with other people, they could use it on their own computer without having to modify the path.
Edit: Why did you use std::wcout instead of std::cout? I read that I should only be using one or the other in my program:
"A program should not mix output operations on wcout with output operations on cout (or with other narrow-oriented output operations on stdout): Once an output operation has been performed on either, the standard output stream acquires an orientation (either narrow or wide) that can only be safely changed by calling freopen on stdout."
- http://www.cplusplus.com/reference/iostream/wcout/
I also read wcout supports Unicode I think(?) but why would I need unicode?
My program uses std::cout elsewhere so I need to use std::cout I think for this.
Also I see that you use: L"\n\n" and I'm not sure what that does or if it's possible to use with std::cout.
---------------------------------
To be clear, I tried adding your provided code to my program just to see how it works when using current_path(). And it gave this error in Visual Studio: https://gyazo.com/043872eb06522129355cd405a6a26100
> Why did you use std::wcout instead of std::cout?
On Windows, the character type used for the native (internal) encoding of file system paths is wchar_t;
the strings returned by the string() and generic_string() members are utf-8 encoded strings (converted from the native wide character encoding).
It is easier to use std::wcout with those; some extra work is required to make the windows console accept utf-8 encoded strings.
This snippet uses std::cout to print std:strings containing the representation of the path in various formats:
> Why did you use std::wcout instead of std::cout?
On Windows, the character type used for the native (internal) encoding of file system paths is wchar_t;
the strings returned by the string() and generic_string() members are utf-8 encoded strings (converted from the native wide character encoding).
It is easier to use std::wcout with those; some extra work is required to make the windows console accept utf-8 encoded strings.
This snippet uses std::cout to print std:strings containing the representation of the path in various formats:
The L prefix specifies that it is a wide character string literal (array of wchar_t )
I'm so sorry for the late reply. I thought I responded, but maybe I forgot to click Submit or something. I don't recall what I meant to respond with but in short:
Thank you for responding! And I've learned how to use filesystem as desired. :)
I will mark this as solved. Please Note: I probably won't check this again.