|
Hi every body!
I've got one CString variable with value "Hello world" and now I want to write its value into one text file. what I have to do now? I'm using Visual C++ 2005 and my project type is MFC project. Thank you every much!
Not good at VC++
|
|
|
|
|
what about using std::ofstream ?
|
|
|
|
|
Probably you need WriteString method of CStdioFile , see here
[^].
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.
|
|
|
|
|
thankx you so much..
Not good at C++
|
|
|
|
|
In additional you can use of CFile class
|
|
|
|
|
CStdioFile already inherits CFile
|
|
|
|
|
Yeah
|
|
|
|
|
If you use std::fstream you need to manually cast the CString e.g.
#include <fstream>
#using namespace std;
...
ofstream out("filename.txt");
CString str("this is a test");
out << (LPCSTR) str << endl;
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
what about adding a out.close() at last ?
also, your example works, but i wanted the OP to search a little by himself too... such easy code is not too hard to write when you're pointed in the right direction, what i did in my first reply
|
|
|
|
|
I had to leave something for the OP to find out - he only asked how to write the string to the file after all . I often don't call the .close() function as it happens automatically when the stream goes out of scope.
Actually I was hoping that someone was going to embarass me and tell me what I've been missing all these years writing explicit casts from CStrings to LPCSTR to use std::stream and also printf, TRACE macros etc.
I only posted as finding the need to explicitly cast can be a bit of a trick that is hard to find.
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
hum, i would have gone into C++ cast operators, like static_cast<LPCTSTR>(str) :p
but, was it really necessary to explicitely cast here ? i'm not 100%, but i have no compiler with me, so...
|
|
|
|
|
Well I've been doing it for years but have only recently upgraded from VC6 to VS2005. You've made me do some experiments and it has been improved (fixed?) in VS2005. The following code segment
<br />
{<br />
std::ofstream out("test.txt");<br />
CString str;<br />
str = "This is a test line";<br />
out << str;<br />
}<br />
on VC6 produces one line in the file:
<br />
00423FDC<br />
which I assume is the address of the CString. In VS2005 it works as you would expect. I guess MS finally included a suitable operator<< overload. I can now stop typing all those (LPCSTR) casts! Thanks tox
Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
|
|
|
|
|
Hi,
I have a dialog which contains different controls,and I want to send
a WM_KEYDOWN message to the parent dialog when a control receives a WM_KEYDOWN
message.
In the dialog,I overrided the OnKeyDown() method as follow:
void Frm_WKFailureMatrix::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}
and in the control,I also overrided the OnKeyDown() method , and inside
that handler I call the ::SendMessage(...) method,as follow:
void WkMfcButton::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
int x = 0;
::SendMessage(GetOwner()->GetSafeHwnd() , WM_KEYDOWN , 0 , 0);
}
My problem is that ::SendMessage() accept the third and fourth parameters
as WPARAM and LPARAM(UINT_PTR and LONG_PTR).
How can I convert nChar,nRepCnt and nFlags to WPARAM and LPARAM so the
parent dialog will receive the correct parameters???
With best regards,
Eli
|
|
|
|
|
|
toxcct wrote: static_cast<>()...
Actually, that won't work correctly. The OP actually needs to pack the three parameters back into the wParam and lParam. The MAKELPARAM and MAKEWPARAM macros will do this. Refer to the WM_KEYDOWN doc to determine where each item goes.
Judy
|
|
|
|
|
|
Why are you doing such a messy thing? This way, you will be writing keydown handlers for each and every control. Do you want your code to look like Sphagetti? What if you add two new controls later? Did I not ask you to override the PreTranslateMessage() ? Did you ever try that?
|
|
|
|
|
Hi brahmma,
I know the way of overriding PreTranslateMessage.
The problem is that I've put a breakpoint inside that function but I never
stop there.
My code is :
virtual BOOL PreTranslateMessage(MSG* pMsg);
and the implementation is :
BOOL Frm_WKFailureMatrix::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
int x = 0;
}
return TRUE;
}
However,if i move the focus into a button(by mouse click),the button receives the WM_KEYDOWN message.
1. Is it because of that automatically when I select a window , the focus
is moving into the first control inside that window?
2. As far as I understand , the parrent windw shall receive the WM_KEYDOWN
message and dispatch it to all of its children , isn't it???
Thanks again,
Eli
|
|
|
|
|
if(pMsg->message == WM_KEYDOWN)
{
if(pMsg->wParam == VK_ESCAPE)
AfxMessageBox(_T("You pressed the Escape Key"));
}
This is just an example. Use the virtual key codes to verify which key was pressed. Check out MSDN for virtual key codes.
|
|
|
|
|
Hello,
I have never worked with C++.
I've a project writtern by another person which creates a OCX output.
Actually it's only a class library. Now I want to change the output type to .DLL instead of .OCX.
Any idea how to do it??
|
|
|
|
|
...in the project properties in all probabilities...
|
|
|
|
|
hi toxcct,
I tried but could not find project property which lets me to change the output type.
Can you pls explain the steps how do I do it?
|
|
|
|
|
what IDE do you use (VC6, VS7, VS71, VS8) ?
|
|
|
|
|
VC 6 IDE....
Regards,
Nilesh
|
|
|
|
|
Got to Settings-->Link-->Output File name . Change its extension to .dll.
Prasad
MS MVP - VC++
|
|
|
|