Skip to content

Instantly share code, notes, and snippets.

@azmfaridee
Created March 18, 2010 15:55
Show Gist options
  • Save azmfaridee/336495 to your computer and use it in GitHub Desktop.
Save azmfaridee/336495 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <fstream>
typedef std::basic_filebuf<wchar_t, std::char_traits<wchar_t> > wfilebuf_t;
#define BUFFER_SIZE 512
using namespace std;
int main(void){
// might be helpful
// std::setlocale(LC_ALL, "UTF-8");
wfilebuf_t infile_buffer;
wfilebuf_t outfile_buffer;
infile_buffer.open("unicode-text.txt", ios_base::in);
outfile_buffer.open("output.txt", ios_base::out);
// custom tune the buffer if necessary
// wchar_t line_buffer[BUFFER_SIZE];
// infile_buffer.pubsetbuf(line_buffer, BUFFER_SIZE);
// outfile_buffer.pubsetbuf(line_buffer, BUFFER_SIZE);
// read character by character, we could read through the buffer
// but that is problematic as we loose the ability of EOF check
if (infile_buffer.is_open()) {
// use WEOF instead of EOF
while(infile_buffer.sgetc() != WEOF){
wchar_t ch = static_cast<wchar_t> (infile_buffer.sbumpc());
wcout << ch;
}
}
infile_buffer.close();
// do not use wchar_t*, this causes a deprecation warning
wchar_t output_line[] = L"This is output line.\n";
// write the whole wchar_t array at the same time, in most practical case,
// might need to rely more on the buffer mechanism
if(outfile_buffer.is_open()){
outfile_buffer.sputn(output_line, (streamsize)wcslen(output_line));
}
outfile_buffer.close();
// some more experiement
// create wchat_t array from wstring
wstring wstr = L"This is the string";
wchar_t *a = new wchar_t[wstr.length()+1];
wstr.copy(a, wstr.length(), 0);
wcout << a;
delete a;
// create wstring from wchar_t
wchar_t source[] = L"This is cool";
wstring dest(source);
wcout << dest;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment