一些信息:
运行时会发生什么
我得到预期的字符串"abcd"重复,直到它达到4094个字符的位置.之后所有输出都是这个标志"?" 直到文件结束.
我怎么看待这个?
我认为这不是预期的行为,它必定是某个地方的错误.
你可以测试的代码:
#include <iostream>
#include <fstream>
#include <locale>
#include <codecvt>
void createTestFile() {
std::ofstream file ("utf16le.txt", std::ofstream::binary);
if (file.is_open()) {
uint16_t bom = 0xFEFF; // UTF-16 little endian BOM
uint64_t abcd = 0x0064006300620061; // UTF-16 "abcd" string
file.write((char*)&bom,2);
for (size_t i=0; i<2000; i++) {
file.write((char*)&abcd,8);
}
file.close();
}
}
int main() {
//createTestFile(); // uncomment to make the test file
std::wifstream file;
std::wstring line;
file.open("utf16le.txt");
file.imbue(std::locale(file.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>));
if …
Run Code Online (Sandbox Code Playgroud) 当我使用wifstream将文本文件读取为宽字符串(std :: wstring)时,流实现是否支持不同的编码 - 即它是否可以用于读取例如ASCII,UTF-8和UTF-16文件?
如果没有,我该怎么办?
(我需要阅读整个文件,如果这有所不同)
在Scott Meyers的"Effective STL"一书中,有一个将整个文本文件读入std :: string对象的好例子:
std::string sData;
/*** Open the file for reading, binary mode ***/
std::ifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode
/*** Read in all the data from the file into one string object ***/
sData.assign (std::istreambuf_iterator <char> (ifFile),
std::istreambuf_iterator <char> ());
Run Code Online (Sandbox Code Playgroud)
请注意,它以8字节字符的形式读取.这非常有效.最近虽然我需要读取包含Unicode文本的文件(即每个字符两个字节).但是,当我尝试(天真地)更改它以将数据从Unicode文本文件读取到std :: wstring对象时,如下所示:
std::wstring wsData;
/*** Open the file for reading, binary mode ***/
std::wifstream ifFile (“MyFile.txt”, std::ios_base::binary); // Open for input, binary mode
/*** Read in all the data from the file …
Run Code Online (Sandbox Code Playgroud) 我想写一个std::wstring
文件,需要读取该内容std:wstring
.当字符串为时,会发生这种情况L"<Any English letter>"
.但是当我们有像孟加拉语,卡纳达语,日语等字符,任何类型的非英语字母时,问题就出现了.试过各种选择,如:
std::wstring
为std::string
写入文件并将读取时间读取为std::string
转换为std::wstring
std::wstring
wofstream,这对于母语字母也没有帮助 std::wstring data = L"?????? ?????????";
平台是mac和Linux,语言是C++
码:
bool
write_file(
const char* path,
const std::wstring data
) {
bool status = false;
try {
std::wofstream file(path, std::ios::out|std::ios::trunc|std::ios::binary);
if (file.is_open()) {
//std::string data_str = convert_wstring_to_string(data);
file.write(data.c_str(), (std::streamsize)data.size());
file.close();
status = true;
}
} catch (...) {
std::cout<<"exception !"<<std::endl;
}
return status;
}
// Read Method
std::wstring
read_file(
const char* filename
) …
Run Code Online (Sandbox Code Playgroud) 在尝试从这个答案中读取带有提示的 UTF-16 编码文件时,我遇到了一个问题,即在读取了几千个字符后,getline
-method 开始在垃圾 mojibake 中读取。
这是我的主要内容:
#include <cstdio>
#include <fstream>
#include <iostream>
#include <codecvt>
#include <locale>
int main(void) {
std::wifstream wif("test.txt", std::ios::binary);
setlocale(LC_ALL, "en_US.utf8");
if (wif.is_open())
{
wif.imbue(
std::locale(
wif.getloc(),
new std::codecvt_utf16<wchar_t, 0x10ffff, std::consume_header>
)
);
std::wstring wline;
while (std::getline(wif, wline))
{
std::wcout << wline;
}
wif.close();
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
该test.txt
文件包含FF
,FE
字节顺序标记,后跟 100 行'a'
,每行80秒。这是test.txt
在 *nix上生成的 bash 脚本:
#!/bin/bash
echo -n -e \\xFF\\xFE > test.txt
for …
Run Code Online (Sandbox Code Playgroud) 我在尝试这个:
std::wstringstream wstrStream;
std::wifstream wifStream(str.c_str());
wifStream >> wstrStream;
Run Code Online (Sandbox Code Playgroud)
但我得到了这个编译错误:
error C2664: 'std::basic_istream<_Elem,_Traits>::_Myt &std::basic_istream<_Elem,_Traits>::operator >>
(std::basic_istream<_Elem,_Traits>::_Myt &(__cdecl *)
(std::basic_istream<_Elem,_Traits>::_Myt &))' : cannot convert parameter 1 from
'std::wstringstream' to 'std::basic_istream<_Elem,_Traits>::_Myt &(__cdecl *)
(std::basic_istream<_Elem,_Traits>::_Myt &)'
with
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>
]
and
[
_Elem=wchar_t,
_Traits=std::char_traits<wchar_t>
]
Run Code Online (Sandbox Code Playgroud)
我知道wchar_t没有实现operator >>.
我发现很少有文档和对std :: wifstream的引用.你会怎么用?
我在Windows上使用Eclipse和MinGW工具链(g ++等).我有一个我在darwin上构建的程序,它使用wifstream和wofstream读取和写入文件.该程序的编译和工作在darwin(Mac)上使用eclipse找到...没有我的问题.
当我将代码移动到Windows并尝试使用MinGW工具链和eclipse进行构建时,我在wifstream,wofstream和wcout上遇到编译错误.定义为wstring的变量编译得很好.
例如:
wifstream inFile; inFile.open(argv [2],ios_base :: in);
导致编译错误
..\src\pdConv.cpp:31:错误:在此范围内未声明`wifstream'
这似乎表明编译器认为wifstream是一个变量.我注意到包含文件中没有启用_GLIBCXX_USE_WCHAR_T指令.我是否需要自己定义,或者环境中应该知道这个?如果我手动定义它,似乎我不能在同一程序中使用宽和窄的实现.
这可能是显而易见的,但我一直坚持这个问题很久......哈哈.我在这里错过了什么?