|
Wide file streams in C++ actually (by default) write single byte characters to files (see this Google groups posting[^] for details). The characters you've provided aren't convertible into your default codepage, so it's game over!
I believe that if you imbue[^] your file stream with a locale[^] that has a codecvt facet[^] that converts to some Unicode encoding, then you'll get proper wide character files.
In fact, this code (which uses the appropriate codecvt facet from this CP article[^]) will do just that!:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#include <locale>
using std::codecvt ;
typedef codecvt < wchar_t , char , mbstate_t > NullCodecvtBase ;
class NullCodecvt
: public NullCodecvtBase
{
public:
typedef wchar_t _E ;
typedef char _To ;
typedef mbstate_t _St ;
explicit NullCodecvt( size_t _R=0 ) : NullCodecvtBase(_R) { }
protected:
virtual result do_in( _St& _State ,
const _To* _F1 , const _To* _L1 , const _To*& _Mid1 ,
_E* F2 , _E* _L2 , _E*& _Mid2
) const
{
return noconv ;
}
virtual result do_out( _St& _State ,
const _E* _F1 , const _E* _L1 , const _E*& _Mid1 ,
_To* F2, _E* _L2 , _To*& _Mid2
) const
{
return noconv ;
}
virtual result do_unshift( _St& _State ,
_To* _F2 , _To* _L2 , _To*& _Mid2 ) const
{
return noconv ;
}
virtual int do_length( _St& _State , const _To* _F1 ,
const _To* _L1 , size_t _N2 ) const _THROW0()
{
return (_N2 < (size_t)(_L1 - _F1)) ? _N2 : _L1 - _F1 ;
}
virtual bool do_always_noconv() const _THROW0()
{
return true ;
}
virtual int do_max_length() const _THROW0()
{
return 2 ;
}
virtual int do_encoding() const _THROW0()
{
return 2 ;
}
} ;
int _tmain(int argc, _TCHAR* argv[])
{
wofstream of("./a.txt",std::ios::out | std::ios::binary);
std::locale l( std::locale(), new NullCodecvt);
of.imbue( l ) ;
of << "Hello";
of.close();
return 0;
}
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
thank you for your help,now my program works fine.
But because I am new to c++, I can't understand the [the article] or the class NullCodecvt well!
Would you mind explain that more ?
What should I do to recognize if a file is UTF-8 or UTF-16 or some other locales, When reading the file.
|
|
|
|
|
C++ treats a disk file as a stream of bytes (makes sense really). The codecvt converts each char (for ofstreams) or wchar_t (for wofstreams) to bytes. This codecvt facet just converts wchar_t (as it's for wofstream) to two bytes (i.e. UTF-16, or UCS-2, whichever encoding Windows Unicode files use) which then get written out to disk.
Amir_m wrote: What should I do to recognize if a file is UTF-8 or UTF-16 or some other locales, When reading the file.
I've got no idea - sorry!
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank You for your explanation.
for an application to recognize a file encoding there were some articles in MSDN
[articles in MSDN] which says: The preferred place to specify byte order is in a file header, but text files do not have headers. Therefore, Unicode has defined a character (U+FEFF) and a noncharacter (U+FFFE) as byte order marks. They are mirror byte images of each other.
and then continues: When an application finds U+FEFF at the beginning of a text file, it typically processes the file as a Unicode file, although it can also perform further heuristic checks to verify that this is true. Such a check can be as simple as testing to find out if the variation in the low-order bytes is much higher than the variation in the high-order bytes. For example, if ASCII text is converted to Unicode text, every second byte is zero. Also, checking both for the linefeed and carriage-return characters (U+000A and U+000D) and for even or odd file size can provide a strong indicator of the nature of the file.
When an application finds U+FFFE at the beginning of a text file, it interprets it to mean that the file is a byte-reversed Unicode file. The application can either swap the order of the bytes or alert the user that an error has occurred.
But there is question of which method to use at the begining of reading a file for recognition,
do we ought to use a special sequense or sth else ...
if there is any one who has experiences about the case please replay the matter, and if there is some codes about this (that makes the job easy) please let us khnow that.
|
|
|
|
|
I'd probably map the file[^], then use IsTextUnicode[^] to decide what encoding it was.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|
|
Thank you good suggestion,Of course my mean was to do the things by pure STL methods.
|
|
|
|
|
Just wanted to check whether the code here in VerifyMaterial() is correct, it works, but as I learned from my last post that doesn't necessarily mean it is correct. I searched google for information but couldn't find anything specific, however any info I found seemed to be against a cast from DWORD to int.
BasicMaterial::BasicMaterial()
{
Ambient = NULL;
Diffuse = NULL;
Specular = NULL;
}
BOOL BasicMaterial::VerifyMaterial()
{
if (!(int)Ambient || !(int)Diffuse || !(int)Specular)
return false;
return true;
}
|
|
|
|
|
Mikey_H wrote: Just wanted to check whether the code here in VerifyMaterial() is correct
To be able to verify that, we need to know what you are trying to do...
Anyway, the cast is not necessary here because a DWORD is an unsigned long. Thus checking if it is null doesn't require you to cast it to an int.
|
|
|
|
|
I want to check to see if the DWORDs have been set from an outside class.
I had originally tried it without the cast, but that did not work.
typedef DWORD D3DCOLOR;
It is actually a D3DCOLOR but I assume the same rules apply?
|
|
|
|
|
Mikey_H wrote: I want to check to see if the DWORDs have been set from an outside class.
And what if they are set as 0 ? The only thing you can verify is check if they are different than 0.
Mikey_H wrote: but that did not work.
Which means what exactly ?
Anyway, if I have to check for if a value is different than zero, I use this notation: if (myValue!=0) , which is a bit clearer (just a matter of taste).
|
|
|
|
|
I've tried again without the cast
if (!Ambient || !Diffuse || !Specular)
return false;
which is working, I obviously didn't have it set up how I thought I did at first.
Checking to make sure value is not 0 is fine, I just want to see if it has been "attempted" to be set, the variables will hold color values so they should not be 0 once set (I think).
Thank you for your help Cedric
modified on Tuesday, April 21, 2009 1:23 PM
|
|
|
|
|
By the way, your code returns false if one of the values is different than 0, is that what you want ? I would guess you want the opposite no ?
Also, with numerical values, it is a bit clearer to consider them as numerical values, not boolean. Logically, it doesn't make any difference but it makes the code a bit more understandable (you want to check that the value is different than 0).
|
|
|
|
|
Function should return true if all 3 values do not equal 0
|
|
|
|
|
Yes you're right that's what it does.
It's the end of the day here, so my brain has been turned off a couple of hours ago
|
|
|
|
|
Mikey_H wrote: I want to check to see if the DWORDs have been set from an outside class.
Are they public or private to the BasicMaterial class?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
Basically, it's a bad code sytle to assign NULL to a DWORD variable. i think you should initialize them with 0 and later compare/verify them as below:
if (Ambient == 0 || Diffuse == 0 || Specular == 0)
{
return false;
}
|
|
|
|
|
Hi,
I have a diff.dll and would like know the process names that can i use. I haven´t any documentation about the process names only about utilization.
I need to put the DLL into project (LoadLibrary) and Get the process (GetProcess) to compare files in apropriate situations.
Realy, i would like to now how can i get the user-defined process names from any DLL´s without the owner documentation.
Is it possible ?
Someone can help me.
|
|
|
|
|
SNArruda wrote: process names that can i use
You mean the exported functions ? Processes are something completely different.
SNArruda wrote: Get the process (GetProcess)
GetProcAddress maybe ? (where Proc doesn't mean process but procedure)
If you are interested in the list of exported functions, you can have a look at this tool[^]
|
|
|
|
|
I have to remove excess "\\" backslashes in a path. EG. D:\Test\\\\ABC\\\\\XYZ is the string with excess back slashes. I have to format it as D:\Test\ABC\XYZ
Can I know the logic.
Thanks
|
|
|
|
|
You could try tokenizing the string using CString's Tokenize[^] method specifying the slash as delimiter and then adding the parts together with a single slash again, so something like this:
CString path, new_path, path_segment;
...
int Pos = 0;
while (!(path_segment = path.Tokenize("\\", Pos)).IsEmpty())
{
new_path = new_path + path_segment + "\\";
}
You can trim the trailing slash later if you don't need it...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> Life: great graphics, but the gameplay sux. <
|
|
|
|
|
The simplest way, though potentially time consuming, is to call CString::Replace("\\\\", "\\") until it returns zero. I'd prefer copying the string and when a slash is encountered in the source, skipping to the next non-slash.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
How did you get a path in such a format?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
|
|
|
|
|
I have a rectangular bitmap I want to transform into an arbitrary four-sided convex polygon. Is there any simple way of doing this knowing the vertices of the polygon? DirectX or OpenGL are not options.
The entire bitmap should be fitted within the polygon with the original corners at the polygon vertices.
Hopefully someone knows a way which doesn't require me to do all the skew/rotation/translation calculations myself
Thanks in advance
|
|
|
|
|
Niklas Lindquist wrote: I want to transform into an arbitrary four-sided convex polygon.
It is a quadrilateral, isn't it?
Niklas Lindquist wrote: Hopefully someone knows a way which doesn't require me to do all the skew/rotation/translation calculations myself
Oh, noone here, I suppose, would deny you such a amusing task...
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|