开发者

Write C++ file with standard ofstream in UCS-2 LE w/o BOM encoding

When I output a file with standard ofstream it shows ASCII\ANSI encoding in Notepad++ which is I think normal, but I need this in UCS-2 LE w/o BOM. I don't know what I should change in this code - can you help?

It's an message file format(.vmg) with character encoding in UCS-2 LE w/o BOM n thats what i want to create in c++.

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;



int main()
{
double i,j;
stringstream sstream;
cout<<"Number Start from:";
cin>>i;
cout<<"\nNumber ends in:";
cin>>j;


for(i;i<=j;)
{

sstream <<i<<".vmg";
string ss = sstream.str();

ofstream sout(ss.c_str());
sout<<"BEGIN:VMSG"<<'\n'<<"VERSION:1.1"<<'\n'<<"X-IRMC-STATUS:"<<'\n'<<"X-IRMC-BOX:INBOX"<<'\n'<<"X-NOK-DT:20101224T190106Z"<<'\n'<<"X-MESSAGE-TYPE:SUBMIT"<<'\n'<<"BEGIN:VCARD"<<'\n'<<"VERSION:3.0"<<'\n'<<"N:"<<'\n'<<"TEL:"<<'\n'<<"END:VCARD"<<'\n'<<"BEGIN:VENV"<<'\n'<<"BEGIN:VCARD"<<'\n'<<"VERSION:3.0"<<'\n'<<"N:"<<'\n'<<"TEL:6969"<<'\n'<<"END:VCARD"<<'\n'<<"BEGIN:VENV"<<'\n'<<"BEGIN:VBODY"<<'\n'<<开发者_如何转开发"Date:24.12.2010 19:01:06"<<'\n'<<"bid "<<i<<'\n'<<"END:VBODY"<<'\n'<<"END:VENV"<<'\n'<<"END:VENV"<<'\n'<<"END:VMSG"<<endl;
sstream.str("");
i=i+0.01;
}
return 0;
}


C++ std::strings have no explicit encoding (they are just containers of char).

You need to determine a couple of things:

  • The encoding used internally.
  • The encoding used externally.

Then you will know how you can convert between the two.

It is useful to pick a fixed width internal representation, like UTF-16 or UTF-32 (I know technically UTF-16 is not fixed width but UCS-2 is and it's close enough).

The external representation need not be fixed width but you seem to want UCS-2 (UTF-16). So if you pick an internal format that matches the external format then no translation is required and you just spew the string to the stream.

If there is a discrepancy between your internal and external representation (Like LE -> BE) you need to convert between the two. To do this use the codecvt facet and imbue the file stream with an appropriate locale. Instructions can be found here: writing-utf16-to-file-in-binary-mode

Edit:

You know the internal encoding (since you made a choice).

External encoding: That will depend on the file:

If you created the file, you will know its encoding. If another program created the file you may have to work out the encoding. For example by reading the BOM (Byte Order mark) at the beginning of the file. That way you can tell if it is UTF-8, UTF-16LE, UTF-16BE or UTF-32.

If it is some other encoding, it may be harder to try and work it out or you just have to take a guess.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜