|
I can't exactly understand what you are trying to do.
For copying files you can use CopyFile[^] API.
OK I'm not going into the implementation
In my knowledge it's called Byte Order Mark (BOM) which identifies the type of encoding applied to the (text) file. If you are going to rename the file, you dont have to bother about anything Windows Platform will identify it and gives correct name for that.
Please check this article.[^]
just open notepad and save text in different formats. Then open it in any hex-editor(probably visual studio can help you out) and verify each encoding.
<br />
Byte-order mark Description <br />
----------------------------<br />
EF BB BF : UTF-8<br />
FF FE : UTF-16, little endian<br />
FE FF : UTF-16, big endian<br />
FF FE 00 00 : UTF-32, little endian<br />
00 00 FE FF : UTF-32, big-endian
--Update--
Check Byte Order Mark [^] on MSDN
HTH
-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin
modified on Wednesday, December 26, 2007 11:10:08 AM
|
|
|
|
|
Thanks a lot!! You are of great help!
|
|
|
|
|
Two suggestions:
(1) always check how many bytes you effectively read and wrote (and use that info).
(2) use the debugger.
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.
[my articles]
|
|
|
|
|
|
Ajay L D wrote: WriteFile(fpNew, str, sizeof(str), &dwNumBytesWritten, NULL);
Should be:
WriteFile(fpNew, str, dwNumBytesRead, &dwNumBytesWritten, NULL);
"Normal is getting dressed in clothes that you buy for work and driving through traffic in a car that you are still paying for, in order to get to the job you need to pay for the clothes and the car and the house you leave vacant all day so you can afford to live in it." - Ellen Goodman
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Hello everyone,
Please help to comment whether my following understanding is correct,
1. whether or not we are using auto_ptr to allocate new object on heap (using new), there may be bad_alloc exceptions;
2. when we met with such exceptions, we catch it (bad_alloc) and try to mininize the operation in catch handler block (since when bad_alloc occurs, it means memory is running out, we can not do anything complex in handler).
Both are correct? Please feel free to correct me if I am wrong.
thanks in advance,
George
|
|
|
|
|
George_George wrote: 1. whether or not we are using auto_ptr to allocate new object on heap (using new), there may be bad_alloc exceptions;
IMHO this is not true, if you use, for instance, malloc (of course) exception is never thrown.
George_George wrote: when we met with such exceptions, we catch it (bad_alloc)
We have the possibility of handling it.
George_George wrote: and try to mininize the operation in catch handler block (since when bad_alloc occurs, it means memory is running out, we can not do anything complex in handler).
That isn't exactly true: the request may fail for a huge chunk request and anyway, complex things don't necessarily need a lot of memory and you can also rely on previous allocated memory to do complex things. However, IMHO, "be minimalist!" on such situation, it is a good guideline.
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.
[my articles]
|
|
|
|
|
Thanks CPallini,
If we met with bad_alloc, do we need to use delete or delete[] to free the memory in exception handler block?
regards,
George
|
|
|
|
|
George_George wrote: If we met with bad_alloc, do we need to use delete or delete[] to free the memory in exception handler block?
You don't need to free unallocated memory, qutoing MSDN
http://msdn2.microsoft.com/en-us/library/6512dwes(vs.80).aspx[^]:
The class describes an exception thrown to indicate that an allocation request did not succeed.
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.
[my articles]
|
|
|
|
|
Thanks CPallini,
But from the link, I can not find it is explicitly mentioned we do not need to delete or delete[] if bad_alloc occurs.
regards,
George
|
|
|
|
|
George: "No memory, no party!"
If bad_alloc is thrown then memory is NOT ALLOCATED then you have not to call neither delete nor delete [] .
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.
[my articles]
|
|
|
|
|
Thanks CPallini,
My question is answered.
regards,
George
|
|
|
|
|
my complie tool is visual studio6.0.
but some source is made from turbo c.
so i can't complie on the visual studio.
"textattr" <-- that function.
so there are any same function with "textattr" on the visual studio library?
firstly, i am sorry, i am from korea.
and i am not good in wriitting english so please understand~
Thank you everyone~
|
|
|
|
|
AFAIK there isn't a similar function for console programs. On the other hand you have a great control on text attributes of windowed applications, but you have to deal with several issues (event-driven apps, graphical output).
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.
[my articles]
|
|
|
|
|
I have created object of tree control as m_TreeCtrl. I want to refresh the tree control on refresh button click to reflect the new changes after adding or deleting the node. I could not find the API for that.
Please help me to find the API for refreshing the tree control.
|
|
|
|
|
Unless you explicitely block, the tree control refreshes itself after inserting/modifying/removing items.
|
|
|
|
|
Hi All,
I have to handle text files of larger sizes ranges from 10GB and more which are exported from some application softwares. The text files contains the required information scattered throughout the file. I need to gather all those information in a particular format to do further analysis. Say for example, if the input file contains as following:
#10 // time stamp 10
0!
1(
1=
#15 // time stamp 15
1!
0:
I have to gather the information as,
! 10 0 15 1
( 10 1
= 10 1
: 10 0
So please suggest me how to store the above gathered information either as a vector, class arrays, or anything else like database e.t.c., The information surely occupies size larger than 10GB. And i have to access the gathered information for further progress.
With Regards,
Sangeetha.
|
|
|
|
|
Hi...
I am working with a Dialog based application. In which i am trying to add features like ptint, help etc. we can achieve this by making CFormView is the base class before making a SDI application. But I dont want the tittle bar & menu. Anybody suggest way of doing this.....
Thanks in advance
sairam
|
|
|
|
|
Hello everyone,
Through my testing and study of RAII (Resource Acquisition Is Initialization) pattern, I think it can solve resource release issue in the following two situations,
1. Local function object (on stack);
2. Object (either on heap or stack) pointer by auto_ptr;
But it has the limitation that the object pointed by a normal pointer and allocated on heap (using new or new[]) can not be automatically released, either the function returns normally or during exception stack unwinding. Is that correct?
thanks in advance,
George
|
|
|
|
|
You can. You could write your own classes or - as I'd recommend - use boost smart pointers.
basically, there is:
scoped_ptr<T> pT(new T) - freed when pT leaves scope
scoped_array<T> paT(new T[77]) - freed when paT leaves scope
shared_ptr<T> pT(new T) - can be assigned to another shared_ptr and deletes T when the last reference goes out of scope
shared_array<T> pT(new T[77]) - as above, but uses delete[]
With shared_ptr you can use a custom deleter, which allows to wrap arbitrary ressources, e.g. malloc'ed or GlobalAlloc'ed pointers.
I've written two articles that give a bit more detail:
Smart Pointers to boost your code[^] for a thorough introduction
shared_ptr wraps resource handles[^] which demonstrates a slightly quirky but efficient way to use the custom deleter.
|
|
|
|
|
Thanks peterchen,
shared_ptr is good idea. But I think it is not implemented in Visual Studio, right?
BTW: any comments/options about our original question (put your good ideas about shared_ptr aside for a while)? Is my understanding correct?
regards,
George
|
|
|
|
|
George_George wrote: But I think it is not implemented in Visual Studio, right?
Yes, it's part of the boost libraries - however, it's one part that is likely to make it into the next C++ Standard (C++ 0x).
George_George wrote: any comments/options about our original question (put your good ideas about shared_ptr aside for a while)? Is my understanding correct?
I thought I answered that But ok, in more detail:
Many resources like heap allocated pointers are not deleted automatically when leaving scope. However, they can be when using the right tools.
You could introduce a helepr class like this:
template <typename t="">
class ScopePtr
{
T * m_pT;
public:
ScopePtr(T * pT) : m_pT(pT) {}
~ScopePtr() { delete m_pT; }
T * ptr() { return m_pT; }
}
</typename>
and use it like this:
void Foo()
{
ScopePtr<bar> bar(new Bar());
bar.ptr()->Run();
...
}
</bar>
If you add some operator overloads and a lot of insight into the trickier pitfalls of C++ and templates, you end up wiht scoped_ptr.
You can do something similar for each and every type of resource.
However, the need is recognized by many developers, so generic solutions exist (see - I can't put aside the thought of shared_ptr )
There are a few other options, too, like using ScopeGuards[^]
|
|
|
|
|
Thanks peterchen,
Cool trick.
regards,
George
|
|
|
|
|
I am using two functions
-------------------------
STDMETHODIMP get_Path(BSTR *pbstrPath);
STDMETHODIMP put_Path(BSTR pbstrPath);
my piece of code is
----------------------
_bstr_t m_bstrNewPath;
CHAR szFileName[MAX_PATH] = {0};
ZeroMemory(szFileName, sizeof(szFileName));
Edit_GetText(GetDlgItem(m_Dlg, IDC_PathEditCtrl),szFileName, MAX_PATH);
m_bstrNewPath = szFileName;
when i am calling the function it is ok
put_Path(m_bstrNewPath);
but when I am calling the function
get_Path(m_bstrNewPath);
it fires error
Cannot convert '_bstr_t' to 'wchar_t * *'
so please help me how to fix that bug
Rajesh
|
|
|
|
|
The safe way is to:
BSTR temp = 0;
HRESULT hr = get_Path(&temp);
m_bstrNewPath.Attach(temp);
The reason is that get_Path allocates the string and needs to return you a pointer, so the parameter uses double indirection of (chartype) ** .
_bstr_t, for various reasons, cannot safely wrap this operation.
There are a few circumstances where you can avoid the temporary (but you have to know exactly when - and when not). So rather play it safe and use above construct. There is no performance hit involved.
in get_Path, path is an [out] only parameter (i.e. the original valeu is ignored).
If you have a method with an [in/out] parameter, use:
_bstr_t path = ...;
BSTR temp = path.Detach();
HRESULT hr = comObject->ModifyPath(&temp);
path.Attach(temp);
if (FAILED(hr))
{
}
note the different order of attaching vs. error handling.
|
|
|
|
|