|
In my application I want to access files, where the underlying character type (ASCII or Unicode) is transparent for the application.
Suppose that in a reporting module a report is written to file, like this:
myfile << reportHeader << std::endl;<br />
for (all columns) myfile << columnname;<br />
myfile << std::endl;<br />
for (all records)<br />
{<br />
for (all columns) myfile << data;<br />
myfile << std::endl;<br />
}<br />
Now, I want my application to transparently write the report to ASCII or Unicode files, depending on the user specification.
Currently, you have to do it like this:
std::ofstream asciiFile;<br />
std::wofstream unicodeFile;<br />
if (user wants Unicode)<br />
{<br />
unicodeFile.open ("output.txt");<br />
unicodeFile << reportHeader << std::endl;<br />
}<br />
else<br />
{<br />
asciiFile.open ("output.txt");<br />
asciiFile << reportHeader << std::endl;<br />
}<br />
for (all columns) <br />
{<br />
if (user wants Unicode) unicodeFile << columnname;<br />
else asciiFile << columnName;<br />
}<br />
... and so on<br />
The disadvantages seem obvious:
- clumsy, unreadable code (especially if the write-to-file logic is spread over several methods)
- writing Unicode strings (std::wstring) to an Ascii stream doesn't even work; it produced garbage
Therefore, I need a kind of transparent stream, so that I can write the following:
transparentstream myfile;<br />
myfile.setMode (ascii or unicode);<br />
myfile.open ("output.txt");<br />
myfile << reportHeader << std::endl;<br />
for (all columns) myfile << columnname;<br />
myfile << std::endl;<br />
for (all records)<br />
{<br />
for (all columns) myfile << data;<br />
myfile << std::endl;<br />
}<br />
Problems are:
- from which stream do I inherit the transparentstream?
- how do I define the transparentstream so all defined output operators keep on working?
- where do I put the conversion logic? (basic_buf? basic_filebuf?)
Of course I want something similar for input, where the stream can find out itself whether the file is Unicode (starts with 0xfffe or 0xfeff) or plain Ascii.
And as an additional challenge: is it possible to have such a transparent stream that can do something like this?
transparentstream mystream;<br />
mystream.open("http://www.mywebsite.com/mypage.html");<br />
mystream >> ...;<br />
And if this is possible, how do I implement such a stream of buf class?
Did some of you already encounter problems trying to mix the STL and Unicode files?
How did you solve it?
Thanks for you suggestions.
Enjoy life, this is not a rehearsal !!!
|
|
|
|
|
Are you referring to overloaded << and >> operators?
ofstream &operator<<(ofstream &of, const CData const &data)
ifstream &operator>>(ifstream &if, const CData const &data)
Kuphryn
|
|
|
|
|
Not exactly.
I want to write to (or read from) a stream without having to worry about whether the file is Ascii or Unicode.
Afaik, STL forces me to know beforehand what the file is and use either an ofstream (for Ascii) or a wofstream (for Unicode), which means that all output statements should be doubled in my application.
In my original post I gave a simple example for a reporting module.
The first code examples shows how to do it in Ascii only.
The second example shows how STL currently forces me to write this module if I want to support both Ascii and Unicode in my application.
The third example shows how I would hope to write it, at least if somebody has a brilliant idea.
Enjoy life, this is not a rehearsal !!!
|
|
|
|
|
You determine the file type via input?
One solution is to overload the operators as mentioned. There could be multiple objects (CObject and CObjectUnicode).
Kuphryn
|
|
|
|
|
Streams are transparent, you should use locale codecvt facet so that the buffer will be converted before saving and after loading.
Your code should look like this:
<br />
std::wofstream myfile;<br />
std::locale loc(std::locale(),new std::codecvt<wchar_t,ascii ? char : wchar_t,std::mbstate_t>());<br />
myfile.imbue(loc);<br />
<br />
<br />
myfile.open ("output.txt");<br />
myfile << reportHeader << std::endl;<br />
for (all columns) myfile << columnname;<br />
myfile << std::endl;<br />
for (all records)<br />
{<br />
for (all columns) myfile << data;<br />
myfile << std::endl;<br />
}<br />
Note that myfile is always wchar_t and codecvt converts the caracters to char.
If your internal data is char based you should use a char based stream and imbue it with a codecvt that converts from char to wchar_t is you need to save Unicode.
Also when saving to Unicode you should write 0xEF 0xBB 0xBF first to indicate the file is Unicode (is what you will find at the beginning of utf-16 xml files) and when reading the file read the first 3 bytes, test them and then use the right codecvt facet to convert to your internal data.
|
|
|
|
|
Very interesting.
Kuphryn
|
|
|
|
|
I wish to add single string to the start of a file.
I used:
_file.seekp(0); // move to start of file
_file << something << std::end;
It overrides the text in start of file, but I wish to insert before current text.
Does anyone know how to do it?
THANKS!
Keep it simple
|
|
|
|
|
SimpleProgramer wrote:
I wish to add single string to the start of a file.
I used:
_file.seekp(0); // move to start of file
_file << something << std::end;
It overrides the text in start of file, but I wish to insert before current text.
Does anyone know how to do it?
There is nothing before 0 (which is even a philosophical answer ). Hint: You can write a new file ....
|
|
|
|
|
Hi Guys.
I have a problem about the max dimension of an element that can be used with the container "deque" defined in STL.
I declare a deque typedef with elements of 2049 bytes (but could be everykind of data or class) :
typedef struct
{
char stringa[2049];
} t_Elem;
typedef deque <t_elem> t_Queue;
I populate an instance of this deque with 5 strings :
t_Queue stQueue;
t_Elem stElem;
//
strcpy (stElem.stringa, "Stringa1");
stQueue.push_back (stElem);
strcpy (stElem.stringa, "Stringa2");
stQueue.push_back (stElem);
strcpy (stElem.stringa, "Stringa3");
stQueue.push_back (stElem);
strcpy (stElem.stringa, "Stringa4");
stQueue.push_back (stElem);
strcpy (stElem.stringa, "Stringa5");
stQueue.push_back (stElem);
Then I try to navigate inside deque, using a standard iterator :
t_Queue::iterator p = stQueue.begin();
while (p != stQueue.end())
{
t_Elem localElem;
strcpy (localElem.stringa, ((t_Elem)(*p)).stringa);
p++;
}
But the stQueue.end() operator doesn't stop my loop after 5 cycles !!!! ... then an Access Violation occour when I use the sixth element of deque.
The strange thing is that if I DECLARE the ' t_Elem' string of 2048 bytes instead of 2049, everything's OK !!!
I know that I could solve the problem using string container instead of pure char[204?] data, but the problem occour even if I declare as element a class that has 2049 bytes of data.
I can navigate through deque using a "for (short i=0; i
|
|
|
|
|
Use a std::string (or even a std::vector<char> ) rather than an array of characters?
Stuart Dootson
'Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p'
|
|
|
|
|
The point of the problem is not "use another class to store string". The problem also occour if I use whatever else element 2049 bytes long. Think for example to a Class with members and methods bigger than 2049 bytes.
But I've got an update. Go to here :
http://www.dinkumware.com/vc_fixes.html
Download the new <deque> header and follow instructions. This fixes the problem. There are also many bug fixes about other STL containers... This Site has been signaled me by Paul McKenzie, Elite Member on the CodeGuru Forum Site.
Problem's now solved. Bye to all, Mirko.
|
|
|
|
|
Hi,
I have a toolbar where I dynamically create the buttons. It uses the TBSTYLE_EX_MIXEDBUTTONS style. Everything seems to work fine, however when I resize my window, there is no drop-down on the RHS ( ">>" ) to access the buttons that have been clipped.
Buttons without text work fine. Obviously I am missing something. Here is my code to create the rebar:
<br />
HWND hWndCustomToolBar = CreateSimpleToolBarCtrl(m_hWnd, IDR_CUSTOM, FALSE, ATL_SIMPLE_TOOLBAR_PANE_STYLE | TBSTYLE_LIST & ~TBSTYLE_TOOLTIPS);<br />
CToolBarCtrl = hWndCustomToolBar;<br />
m_customToolbar.SetImageList(*m_sysImageList.GetImageList());<br />
m_customToolbar.SetExtendedStyle(TBSTYLE_EX_MIXEDBUTTONS);<br />
m_customToolbar.SetMaxTextRows(1);<br />
m_customToolbar.AutoSize();<br />
CreateSimpleReBar(ATL_SIMPLE_REBAR_NOBORDER_STYLE);<br />
AddSimpleReBarBand(hWndCustomToolBar, NULL, TRUE);<br />
Then to add my buttons I use the following styles:
<br />
button.fsState |= TBSTATE_ENABLED;<br />
button.fsStyle |= TBSTYLE_AUTOSIZE | BTNS_SHOWTEXT;<br />
m_customToolbar.AddButtons(1, &button);<br />
Is there anything else I need to do?
Thanks in advance.
|
|
|
|
|
Is there a standard notation in use for STL containers/iterators? If not, what are you people using?
For example,
<br />
typedef vector<Blah> BlahArray;<br />
<br />
typedef vector<Blah>::iterator iBlahArray;<br />
typedef vector<Blah>::const_iterator ciBlahArray;<br />
typedef vector<Blah>::reverse_iterator riBlahArray;<br />
typedef vector<Blah>::const_reverse_iterator criBlahArray;<br />
Pssst. You see that little light on your monitor? That's actually a government installed spy camera. Smile and wave to big brother!
|
|
|
|
|
I don't use the typedef thing at all, I think it's ridiculous, and there are better ways to obsfucate ones code, if you really want to.
Christian Graus - Microsoft MVP - C++
|
|
|
|
|
I don't typedef my STL containers, I typically declare them as one might a variable; encapsulated in a class.
<br />
class MyIntClass<br />
{<br />
....<br />
<br />
private:<br />
list<int> intList;<br />
list<int>::const_iterator intListIterator;<br />
<br />
....<br />
<br />
};<br />
Maybe a better question is what is the advantage of one over the other, then apply that to your programming situation ...
:..::. Douglas H. Troy ::..
Fold with us|Development Blogging|viksoe.dk's site
|
|
|
|
|
Sorry -I posted this to C++ forum but it may be more appropriate here.
Hi,
I've trying to create a browser helper object (BHO) for explorer. I've got a main "manager" class that initializes two objects. Each of these uses an simple XML parser (using MSXML). Now, when parsing XML files, I want to store the results to a struct array (not a class). The struct array is in turn comprised of a few other struct arrays. All mem allocation is static, only the topmost parent struct array is dynamically allocated. That's where the problem is: When I allocate (using new) the struct arr in the first object, its ok. Then when I init the second obj, the new fails and crushes the explorer. I did a simple test in the second obj:
int *i = new int[x];
when x is <255 it's ok. if x>=255 then it crashes (and of course, my struct arr is bigger than 255*sizeof(int) bytes). I don't think I have any serious mem leaks in the code before this.
Has anyone seen this before? Is there a memory limit imposed by explorer (unlikely as it sounds)? I would appreciate any comments.
Thanks,
Pete
|
|
|
|
|
|
I thought that it would be like this. Still, I can't understand why I cannot exceed a limit when allocating memory (except if I have a really serious leak -but when IE is loading, memory consumption looks normal).
Thanks.
|
|
|
|
|
I ported my VS2003 PocketPC WTL project to VS2005 and I have the problems
with resources.
When I modify a Dialog in my resource editor I get an error
Error 1 error RC2104 : undefined keyword or key name: DS_FIXEDSYS
Any idea?
Thanks
|
|
|
|
|
|
Winuser.h reveals:
#if(WINVER >= 0x0400)
#define DS_FIXEDSYS 0x0008L
Good music: In my rosary[^]
|
|
|
|
|
|
Hi,
I have to store 10 strcture in vector.
struct
{
int
int
int
int
};
Can you please help me. I am new to stl programming.
Thanks
|
|
|
|
|
use the <pre> tag to format your post correctly, b'cause we can't see anything on your question.
moreover, what is exactly your request ?
TOXCCT >>> GEII power [toxcct][VisualCalc]
|
|
|
|
|
Vectors can hold pretty much anything with a name, so if you have your struct defined like:
struct myStructure {<br />
int one;<br />
int two;<br />
int three;<br />
int four;<br />
};
then just define a vector of myStructs like so:
vector<myStructure> myVector;<br />
myVector.push_back(firstStructure);
then you can just access your data like you would with a basic array, ie
myStructure temp;<br />
temp=myVector[5];
or you can modify in place like
myVector[5].one=37;
Pretty cool stuff.
|
|
|
|