c++ - 如何在c ++ ofstream中打开包含日语单词的文件路径?
问题描述
我正在使用 c++ 中的 ofstream 编写输出文件,文件路径是 c:\my_folder\フォルダ\text_file.txt
但它显示找不到路径,这是我的代码(我在 Visual Studio 2017 社区版中尝试过)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile;
outfile.open(c:\\my_folder\\フォルダ\\text_file.txt,ios::out);
if(!outfile)
{
cout<<"Path not found\n";
}
outfile << "Hello world\n";
outfile.close();
return 0;
}
和
我试过这样也参考 - Displaying Japanese characters in visual c++
#include <string>
#include <iostream>
#include <windows.h>
int main()
{
int codepage = CP_ACP; //CP_ACP, CP_OEMCP
int conv_codepage = CP_UTF8; //CP_UTF8
char str[256];
char str1[256];
wchar_t tstr[256], tstr2[256];
memset(str, 0x00, 256);
memset(str1, 0x00, 256);
memset(tstr, 0x00, 256);
memset(tstr2, 0x00, 256);
memcpy(str, "c:\\my_folder\\フォルダ\\text_file.txt", sizeof(str));
int nLen = MultiByteToWideChar(codepage, 0, str, -1, 0, 0);
MultiByteToWideChar(codepage, 0, str, -1, tstr, nLen);
int len = WideCharToMultiByte( conv_codepage, 0, tstr, -1, NULL, 0, 0, 0 );
WideCharToMultiByte(conv_codepage, 0, tstr, -1, str1, len ,0 ,0);
cout << "2... " << str1 << endl;
ofstream outfile;
outfile.open(str1,ios::out);
if(!outfile)
{
cout<<"Path not found\n";
}
outfile << "Hello world\n";
outfile.close();
return 0;
}
我也尝试过使用 wchar_t 但它们都不起作用,
std::wcout.imbue(std::locale("ja_jp.utf-8"));
wchar_t c_name = L"c:\\my_folder\\フォルダ\\text_file.txt";
wofstream outfile;
outfile.open(c_name,ios::out );
if(!outfile)
{
cout<<"Path not found\n";
}
outfile << "Hello world\n";
outfile.close();
请帮我。
解决方案
推荐阅读
- flutter - V/PlayerBase(2113): baseStop() piid=23127 Flutter
- php - 如何使 MySQL INTO OUTFILE 输出可按组/每个人写入?
- dpdk - 在 mbuf 中嵌入缓存对齐的元数据
- python - 使用 Python Paramiko 通过双 SSH 隧道连接到数据库
- javascript - Vue apexcharts Y轴标签隐藏在水平条形图中
- r - 计算列的平均值以及下一列中的注释
- salesforce-marketing-cloud - 联系人密钥 v/s 用户密钥
- python - 在不和谐经济系统中未触发使用命令
- html - HTML 作业问题 - Flexbox - 初学者
- swift - 使用 Swift 在 macOS 上围绕光标绘制形状