Как правильно вывести в консоль названия файлов на кириллице полученные с FTP сервера с помощью FtpFindFirstFile и InternetFindNextFile?
Если название на кириллице то выводится вот что Р?Р?Р?С<Р№ РєР°С'Р°Р>Р?Р?.
FTP сервер локальный на Windows 10 русская версия.
Мой код состоит из двух файлов:
main.cpp:
#include <iostream>
//#include <io.h>
//#include <fcntl.h>
#include <Windows.h>
#include <WinInet.h>
#include <vector>
#pragma comment(lib,"wininet")
#include <stdlib.h>
#include "scandir.cpp"
using namespace ::std;
int main()
{
setlocale(LC_ALL, "Russian");
cout << "Старт" << endl;
/*if (
!_setmode(_fileno(stdout), _O_U8TEXT)
|| !_setmode(_fileno(stdin), _O_U8TEXT)
|| !_setmode(_fileno(stderr), _O_U8TEXT)
)
{
wcout << "Unable to set mode" << endl;
return 0;
}*/
HINTERNET hInternetRoot = InternetOpen("Mozilla", INTERNET_OPEN_TYPE_PRECONFIG, nullptr, nullptr, NULL);
if (!hInternetRoot) {
cout << "InternetOpen error: " << GetLastError() << endl;
return 0;
}
HINTERNET hInternetConnection = InternetConnect(hInternetRoot, "localhost", INTERNET_DEFAULT_FTP_PORT, "username", "password", INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, NULL);
if (!hInternetConnection) {
cout << "InternetConnect error: " << GetLastError() << endl;
InternetCloseHandle(hInternetRoot);
return 0;
}
string command;
TCHAR currentDir[MAX_PATH];
DWORD currentDirBuffer = MAX_PATH;
if (!FtpGetCurrentDirectory(hInternetConnection, currentDir, ¤tDirBuffer)) {
cout << "FtpGetCurrentDirectory error: " << GetLastError() << endl;
return 0;
}
while (true) {
cout << "FTP>";
cin >> command;
if (command == "scandir") {
vector<string> files;
int filesLen,
i;
files = scanDir(hInternetConnection, currentDir);
filesLen = files.size();
for (i = 0; i < filesLen; i++) {
cout << files[i] << endl;
}
}
else if (command == "setdir") {
}
else if (command == "exit") {
return 0;
}
else {
cout << "Invalid command" << endl;
}
cout << endl;
}
return 0;
}
И scandir.cpp
#include <iostream>
#include <vector>
#include <Windows.h>
#include <WinInet.h>
#pragma comment(lib,"wininet")
#include <stdlib.h>
using namespace ::std;
inline vector<string> scanDir(HINTERNET hInternetConnection, string dir)
{
vector<string> files;
if (!FtpSetCurrentDirectory(hInternetConnection, dir.c_str())) {
DWORD lastError = GetLastError();
cout << "FtpSetCurrentDirectory error: " << lastError << endl;
return files;
}
WIN32_FIND_DATA win32FindData;
HINTERNET hFind = FtpFindFirstFile(hInternetConnection, "*.*", &win32FindData, INTERNET_FLAG_RESYNCHRONIZE, NULL);
if (!hFind) {
cout << "FtpFindFirstFile error: " << GetLastError() << endl;
return files;
}
files.push_back(win32FindData.cFileName);
while (InternetFindNextFile(hFind, &win32FindData)) {
files.push_back(win32FindData.cFileName);
}
InternetCloseHandle(hFind);
return files;
}
Пробовал перевести всю программу на UTF и использовать функции и типы которые начинаются и заканчиваются на W, как описано вот здесь Русский язык в консоли, то пользы не дало, тоже абракадабра только другая.
Причем вот эта программа отлично выводит названия из той же директории:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
int counter = 0;
setlocale(LC_ALL, "russian");
char path[MAX_PATH] = { 0 };
std::cout << "Enter path: ";
cin >> path;
strcat_s(path, "\\*.*");
hFind = FindFirstFile(path, (LPWIN32_FIND_DATAA)& FindFileData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
cout << FindFileData.cFileName << '\n';
} while (FindNextFile(hFind, &FindFileData));
}
FindClose(hFind);
return 0;
}
Я не вижу какие либо принципиальные отличия в поиске и выводе названий файлов в моем коде scandir.cpp и в последней программе.
Думаю у функции FtpFindFirstFile есть какие то особенности.