-
August 9th, 2019, 08:11 AM
#1
How to convert const std::filesystem::directory_entry to tchar?
I'm trying to convert const std::filesystem:irectory_entry (dirent) to tchar but I don't understand how it an be done. I tried a lot of ways. Can you help me?
Code:
#include <memory>
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <tchar.h>
namespace fs = std::filesystem;
int main() try
{
std::for_each(fs::recursive_directory_iterator("./foo/"), {},
[](fs::directory_entry const& dirent)
{
if (fs::is_regular_file(dirent) &&
dirent.path().filename() == "black.txt")
TCHAR path[_MAX_PATH];
const std::filesystem::directory_entry &entry = *path;
_tcscpy(path, dirent);
});
}
catch (fs::filesystem_error const& e)
{
std::cerr << "error: " << e.what() << '\n';
}
-
August 9th, 2019, 11:36 AM
#2
Re: How to convert const std::filesystem::directory_entry to tchar?
Victor Nijegorodov
-
August 9th, 2019, 11:48 AM
#3
Re: How to convert const std::filesystem::directory_entry to tchar?
.filename() returns a type path. To access this as a 'c-style' string, use .c_str(). So something like this (not tried):
Code:
_tcscpy(path, dirent.path().filename().c_str());
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 9th, 2019, 12:59 PM
#4
Re: How to convert const std::filesystem::directory_entry to tchar?
Originally Posted by 2kaud
.filename() returns a type path. To access this as a 'c-style' string, use .c_str(). So something like this (not tried):
Code:
_tcscpy(path, dirent.path().filename().c_str());
Unfortunately it shows errors: https://imgur.com/a/wOhD5ln and "path" is undeclared identifier
-
August 9th, 2019, 01:41 PM
#5
Re: How to convert const std::filesystem::directory_entry to tchar?
Originally Posted by prako2
Have a look at the Format observers section in https://en.cppreference.com/w/cpp/filesystem/path
Victor Nijegorodov
-
August 9th, 2019, 03:58 PM
#6
Re: How to convert const std::filesystem::directory_entry to tchar?
Originally Posted by VictorN
Thanks, I was looking to it before.
-
August 9th, 2019, 05:07 PM
#7
Re: How to convert const std::filesystem::directory_entry to tchar?
I solved windows.h problem by setting up conformance mode to no. I'm trying a code with copy function of windows.h but it doesn't work (path variable).
Code:
#include "pch.h"
#include <Windows.h>
#include <memory>
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <tchar.h>
namespace fs = std::filesystem;
int main()
{
try {
std::for_each(fs::recursive_directory_iterator("d:/folder/"), {},
[](fs::directory_entry const& dirent)
{
if (fs::is_regular_file(dirent) &&
dirent.path().filename() == "black.txt")
fs::copy(dirent, "d:/folder/");
TCHAR path[_MAX_PATH];
_tcscpy(path, dirent.path().filename().string().c_str());
CopyFile(
path,
"d://folder//red//white.txt", // Hardwire the path\filename
TRUE // Do not overwrite it if it already exists.
);
}
);
}
catch (fs::filesystem_error const& e)
{
std::cerr << "error: " << e.what() << '\n';
}
}
I want to copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt but it doesn't work.
-
August 9th, 2019, 01:37 PM
#8
Re: How to convert const std::filesystem::directory_entry to tchar?
This compiles as Multi-byte character set:
Code:
#include <memory>
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <tchar.h>
namespace fs = std::filesystem;
int main()
{
try {
std::for_each(fs::recursive_directory_iterator("./foo/"), {},
[](fs::directory_entry const& dirent)
{
if (fs::is_regular_file(dirent) &&
dirent.path().filename() == "black.txt") {
TCHAR path[_MAX_PATH];
_tcscpy(path, dirent.path().filename().string().c_str());
}
});
}
catch (fs::filesystem_error const& e)
{
std::cerr << "error: " << e.what() << '\n';
}
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 9th, 2019, 03:45 PM
#9
Re: How to convert const std::filesystem::directory_entry to tchar?
Originally Posted by 2kaud
This compiles as Multi-byte character set:
Code:
#include <memory>
#include <filesystem>
#include <algorithm>
#include <iostream>
#include <tchar.h>
namespace fs = std::filesystem;
int main()
{
try {
std::for_each(fs::recursive_directory_iterator("./foo/"), {},
[](fs::directory_entry const& dirent)
{
if (fs::is_regular_file(dirent) &&
dirent.path().filename() == "black.txt") {
TCHAR path[_MAX_PATH];
_tcscpy(path, dirent.path().filename().string().c_str());
}
});
}
catch (fs::filesystem_error const& e)
{
std::cerr << "error: " << e.what() << '\n';
}
}
Thank you, it compiles now. But I encountered more global problem. I'm using v141_xp in vs2017 and I'm getting >c:\program files (x86)\microsoft sdks\windows\v7.1a\include\objbase.h(239): error C2760: syntax error: unexpected token 'identifier', expected 'type specifier' if I add <windows.h>. If using v141 then windows.h is not found at all. And I can't find any solution for this.
-
August 10th, 2019, 02:24 AM
#10
Re: How to convert const std::filesystem::directory_entry to tchar?
-
August 10th, 2019, 03:11 AM
#11
Re: How to convert const std::filesystem::directory_entry to tchar?
a.txt was in my "folder". Now I deleted it and now it shows that "path" is equal to some weird symbols
-
August 10th, 2019, 03:04 AM
#12
Re: How to convert const std::filesystem::directory_entry to tchar?
I just debugged and it says that imgur.com/a/Ul4f4ua and I can't understand why
-
August 10th, 2019, 03:19 AM
#13
Re: How to convert const std::filesystem::directory_entry to tchar?
using / in a path name may work in some places, but \ is the path separator in windows. And in a non-raw text string, then \\ is used. So
"d://folder//" becomes "d:\\folder\" and "d://folder//red//white.txt" becomes ""d:\\folder\\red\\white.txt". If / is used for a path, then you don't use //.
I want to copy d://folder//(unknownDir)//black.txt to d://folder//red//white.txt if white.txt does not exist
Consider:
Code:
#include <filesystem>
namespace fs = std::filesystem;
const fs::path initpath {"c:\\myprogs\\folder\\"};
const fs::path destname {"c:\\myprogs\\folder\\red\\white.txt"};
const fs::path req_fn {"black.txt"};
int main()
{
for (const auto& fn : fs::recursive_directory_iterator(initpath))
if (fs::is_regular_file(fn) && fn.path().filename() == req_fn) {
fs::copy_file(fn.path(), destname, fs::copy_options::skip_existing);
break;
}
}
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
August 10th, 2019, 03:54 AM
#14
Re: How to convert const std::filesystem::directory_entry to tchar?
Originally Posted by 2kaud
using / in a path name may work in some places, but \ is the path separator in windows. And in a non-raw text string, then \\ is used. So
"d://folder//" becomes "d:\\folder" and "d://folder//red//white.txt" becomes ""d:\\folder\\red\\white.txt". If / is used for a path, then you don't use //.
Consider:
Code:
#include <filesystem>
namespace fs = std::filesystem;
const fs::path initpath {"c:\\myprogs\\folder\\"};
const fs::path destname {"c:\\myprogs\\folder\\red\\white.txt"};
const fs::path req_fn {"black.txt"};
int main()
{
for (const auto& fn : fs::recursive_directory_iterator(initpath))
if (fs::is_regular_file(fn) && fn.path().filename() == req_fn) {
fs::copy_file(fn.path(), destname, fs::copy_options::skip_existing);
break;
}
}
Your code crashes for me somehow
-
August 10th, 2019, 04:13 AM
#15
Re: How to convert const std::filesystem::directory_entry to tchar?
Works ok on my system with my test folders. Copies the specified file as required. VS2019, release build, Multi-byte character set.
Is there something special about your folder structure/files? Can you zip the folder directory and its subs and files and post as an attachment?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|