1

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;
}
neonant
  • 832
  • 3
  • 16
  • 25
  • You should probably include '`<< endl`' in the output line. What you are seeking is platform dependent. The mention of Notepad++ suggest Windows, as does UCS-2 LE (probably should be UTF-16LE, though they are basically the same thing). You are likely to need to look in the manuals. It may not be possible; if it is, you are likely to need to turn on wide-character encoding, and using a wide-string literal (`L"Hello World"`) would help, not hinder. – Jonathan Leffler Dec 24 '10 at 21:46
  • What's with this incessant urge to *not* write a BOM to allow another program that reads the file to guess the encoding correctly? Why don't you use EBCDIC? – Hans Passant Dec 25 '10 at 00:14
  • Actually my desired character encoding of output file is:UCS-2 LE w/o BOM.converting string to stream doesn't help nor L"Hello World" does.And what about EBCDIC,how to do that? – neonant Dec 25 '10 at 04:24

1 Answers1

1

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.

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • I am not sure about what are you asking of internal/external encoding.n how to know if there is a descripency? I added my source code please see above.And thanks for the link. – neonant Dec 25 '10 at 04:41
  • Thank you Martin York,but now i know that it's internal encoding then how should i go with the coding?? i have no idea.Your given link doesn't seem to justify to it's title with output though. – neonant Dec 26 '10 at 06:16