This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Qt equivalent of PathAppend?
Short story: does Qt 4 have an analog of Python's os.path.join?
Long story: I need to add a relative path to the application directory, QCoreApplication::applicationDirPath() in the Right Way (TM), so that the code doesn't depend on the file system directory separator character.
Is merely joining QStrings and using "/" as the separator a good solution?
You can either use "/" directly or use QDir::separator(). But in general use a QDir for this (which translates "/" to the platform specific path separator for you).
From Qt 4.6 QDir documentation,
Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.
So, I guess QDir will be helpful for you.
Just use "/" when staying in the Qt world.
To convert the path for non-Qt classes and functions etc., use QDir::toNativeSeparators( path ).
Related
I just noticed that std::filesystem::recursive_directory_iterator uses different path separateors (i.e. / vs \) depending on whether it's on Windows or Linux, is there a way to make it return paths with '/' to make it consistent across systems?
This is how I am getting the paths:
for(auto& path: fs::recursive_directory_iterator(dir_path))
{
// Skip directories in the enumeration.
if(fs::is_directory(path)) continue;
string path_str = path.path().string();
}
What I mean is, the contents of path_str will be different between the two OSs (because the separators will be different), I would like them to be the same. I could just replace them on the final string, but that uses more cycles than if I can instruct the stl to use '/' for everything isntead.
So, your problem has nothing to do with recursive_directory_iterator, which iterates on directory_entry objects, not paths. Your confusion probably stems from the fact that directory entries are implicitly convertible to paths, so you can use them as such.
Your problem is really about path::string(), which, as the documentation states, uses the native format (i.e. with a platform dependent separator). You would get the same problem regardless of how you get your path.
If you want to get / as the directory separator, use path::generic_string() instead to get the path in generic format.
for(auto& dir_entry: fs::recursive_directory_iterator(dir_path))
{
if(dir_entry.is_directory()) continue;
string path_str = dir_entry.path().generic_string();
}
Unix uses / to separate folders but Windows uses \, so I wonder a safe way for cross-platform to concat two paths like /tmp/ + temp_file.xxx.
Just after a minute, I found the answer, File.join (-_-)
This question already has answers here:
How to open a folder in %appdata% with C++?
(6 answers)
Closed 6 years ago.
I need help creating a Folder in AppData.
Lets say I want to create a Folder in %appdata% called "MyFolder", which has the text file test.txt
I tried to use <fstream> and do this;
ofstream file("%appdata%\MyFolder\test.txt");
but it didn't work..
Things like %appdata% are OS specific, and Standard C++ has no direct means of dealing with them. You will have to write code to parse the file path, and extract values like %appdata% from the environment, or alternatively use non-standard functions to open the file, should such exist.
P.S. It also wouldn't work because "\" escapes the quotes, do "\\" instead.
This question already has an answer here:
Why do I have to use double backslashes for file-paths in code?
(1 answer)
Closed 8 years ago.
I have the following folder structure:
/code
/files
(i.e "files" folder is present inside the "code" folder)
I am trying to create a file(and write in it) using of fstream like this:
ofstream out("\files\plsmap.txt");
The compiler isn't giving any error, but, no file is being created in the "files" folder. What can be the possible reason for this and also, is there some other way of doing this
You wrote "\files\plsmap.txt". The leading slash specifies the root directory of your entire file system. So "\files" is very different from "\code\files". You could try removing the leading slash, or changing the string so that it contains the full, absolute path to the directory you are trying to write. You can figure out the absolute path of a directory by running pwd in that directory from a terminal.
Besides the misplaced slashes, the other thing to note is that backslashes are special inside C++ strings, and they need to be escaped using a second backslash when you are writing them in your C++ source code:
const char * path = "code\\plsmap.txt";
Your environment might allow you to use forward slashes instead, which would be easier since they don't need to be escaped.
Is there any path open-source manipulation library which supports all of the following?
Unrestricted path lengths (i.e. the only restriction should be from the range of size_t, not arbitrary limitations like 256 characters)
Basic manipulations like canonicalization, the equivalent of basename, dirname, getting the file extension, getting the root, etc.
All valid Windows-style paths and file names, such as \Rooted, Dir/, C:\Dir/foo, File, \\Computer\Dir/File, \\.\C:, Foo\./.\Bar:ADS, or \\?\C:\Dir\Escaped:ADS:$DATA
I believe this should also cover POSIX-style paths, but if not, those should work too
I'd prefer C++, but C is also fine.
cwalk can do that. It's a small C path library.
Sounds like QDir and QFileInfo from Qt 4.