为什么C++中没有std::wostream_iterator?
这有什么好的理由吗?
#include <fstream>
#include <iterator>
#include <string>
#include <vector>
int main()
{
std::vector<std::wstring> myvec = { L"first", L"second" };
std::wofstream f("New.txt");
// std::copy(myvec.begin(), myvec.end(), std::wostream_iterator<std::wstring>(f)); // Error
// std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring>(f)); // Error
std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring, wchar_t>(f)); // Ok
std::copy(myvec.begin(), myvec.end(), std::ostream_iterator<std::wstring, wchar_t, std::char_traits<wchar_t>>(f)); // Ok
}
Run Code Online (Sandbox Code Playgroud) 我在Linux系统上使用Qt/C++.我需要将一个QLineEdit文本转换为一个文本std::wstring并将其写入std::wofstream.它适用于ascii字符串,但是当我输入任何其他字符(阿拉伯语或乌兹别克语)时,文件中没有任何内容.(文件大小为0字节).
这是我的代码:
wofstream customersFile;
customersFile.open("./customers.txt");
std::wstring ws = lne_address_customer->text().toStdWString();
customersFile << ws << ws.length() << std::endl;
Run Code Online (Sandbox Code Playgroud)
John Smith在行编辑中输入的输出是John Smith10.但对于unicode字符串,什么都没有.
首先我认为这是一个问题QString::toStdWString(),但customersFile << ws.length();写出所有字符串的正确长度.所以我想我在写wstring文件时做错了.[?]
编辑:
我在eclipse中再次写它.并用g ++ 4.5编译它.结果是一样的:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
cout << "" << endl; // prints
wstring ws = L"????"; // this is an Arabic "Hello"
wofstream wf("new.txt");
if (!wf.bad())
wf << ws;
else
cerr << …Run Code Online (Sandbox Code Playgroud) 我正在编写一个程序来获取UCS-2 Little Endian中*.rc文件编码的信息.
int _tmain(int argc, _TCHAR* argv[]) {
wstring csvLine(wstring sLine);
wifstream fin("en.rc");
wofstream fout("table.csv");
wofstream fout_rm("temp.txt");
wstring sLine;
fout << "en\n";
while(getline(fin,sLine)) {
if (sLine.find(L"IDS") == -1)
fout_rm << sLine << endl;
else
fout << csvLine(sLine);
}
fout << flush;
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在"en.rc"的第一行是#include <windows.h>但sLine如下所示:
[0] 255 L'ÿ'
[1] 254 L'þ'
[2] 35 L'#'
[3] 0
[4] 105 L'i'
[5] 0
[6] 110 L'n'
[7] 0
[8] 99 L'c'
. .
. . …Run Code Online (Sandbox Code Playgroud) 可悲的是,这是本周我第三次提出问题.
我必须使用unicode编码(或UTF8)将文本写入文件.
这就是我做的:
创造wofstream mystream;,然后我wstring像这样放入它mystream << L"hello world";
第一个问题:在我的情况下,流使用什么样的编码?
其次,我想加载我的新文件,但如何读取行?该ifstream的getline不工作,因为行结束毁明显.
我在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指令.我是否需要自己定义,或者环境中应该知道这个?如果我手动定义它,似乎我不能在同一程序中使用宽和窄的实现.
这可能是显而易见的,但我一直坚持这个问题很久......哈哈.我在这里错过了什么?
考虑我的日志记录类
void LogWriter::InitLogWriter(void)
{
wcout.flush();
wcerr.flush();
COUT_BACKUP = wcout.rdbuf(); // save original cout buffer
CERR_BACKUP = wcerr.rdbuf(); //save original cerr buffer
FileStreamOpen = false;
switch(m_nTraceLvl)
{
case OffLevel:
StopOutput();
break;
case ErrorLevel:
OutputErrorsToFile(s_TraceFile);
break;
case DetailLevel:
OutputAllToFile(s_TraceFile);
break;
default:
StopOutput();
break;
}
wcout << "Initialize Log Writer" << endl;
}
void LogWriter::OutputAllToFile(TCHAR* fileName)
{
wstreambuf* fb = GetFileBufferStream(fileName);
wcout.rdbuf(fb); // push wcout buffer to file
wcerr.rdbuf(fb); // push cerr buffer to file
FileStreamOpen = true;
}
void LogWriter::OutputErrorsToFile(TCHAR* fileName)
{ …Run Code Online (Sandbox Code Playgroud)