|
Thanks
- Anders
|
|
|
|
|
<<< In the name of GOD >>>
Hi there.
I want to read from file.
I haven't any problem, but for it, i used of CString variable for get data from file.
This variable define in global mode.
Now i want to get data to LPSTR variable, in global mode, but compiler give me error.
This is my code about CString variable:
CString szText; // Global variable
.
.
.
void CEditorDlg::Loading()
{
CFile file;
file.Open("Hadi.txt", CFile::typeBinary | CFile::modeRead);
file.Read(szText.GetBufferSetLength(file.GetLength()), file.GetLength()); // I want to put LPSTR variable instead CString variable.
file.Close();
}
Thanks for reply.
Hadi Rezaie
|
|
|
|
|
Have you considered using CStdioFile?
--
Alex Marbus
www.marbus.net
But then again, I could be wrong.
|
|
|
|
|
hmmmmm.
Ok, i must use CStdioFile for it.
But i want to know which is better and faster to use CFile or CStdioFile and why ???
Thanks.
Hadi Rezaie
|
|
|
|
|
I'm not sure what's faster; using CFile and your own routine to read/write plain text line-by-line, or using CStdioFile and use ReadString and WriteString. If you write your own functions to do this, you'll probably end up doing the same thing as CStdioFile does internally (I haven't looked at the MFC code yet, no development-environment on the machine I'm currently writing this message).
CStdioFile is the right class if you only want to read/write plain text from/to file. It's very easy to use. Saves you a lot of thinking
G'luck
--
Alex Marbus
www.marbus.net
But then again, I could be wrong.
|
|
|
|
|
Ok,
I have two other questions:
Q1. What is Buffer in file that use for writing and reading from file ?
Q2. Can i use of typeBinary flag for open the file in CFile object ?
Hadi Rezaie
|
|
|
|
|
Usage of the GetBufferSetLength member function of CString class is
CString diva("Madonna");
LPTSTR ptrdiva = diva.GetBufferSetLength( 10 );
try using it like this
file.Read((void *)szText.GetBufferSetLength(file.GetLength()), file.GetLength());
as Read requires a (void *) i.e. a void ptr...
Dharne Atul
---------------------------------------
do not rely on any code till ya try it
|
|
|
|
|
Maybe its 3:30 and My Head is Not Straight, But Im having trouble Figureing this one out.
This is my First VC++ App and things are going quite well.
My Problem is I have a CListCtrl that is representing "Tasks" in a Queue. I would like the USer to Be Able to Delete any number of Selected Items in the Queue. So if they Select Five Items and Click "Remove" those Items and Their Corresponding Data will be deleted.
My problem is Navigating Through the Selected Item List.
In Other CListCtrls I use I use the Same Method of moving through the Item List with no Problem.
Whats the best Method for Deleteing Items from a CListCtrl that are Selected.
GetNExtSelected or using the LVIS_SELECTED mask to identify selected Items and remove them?
quick sample code would be appreciated.
recurse.org - Recusion For the Rest Of Us.
|
|
|
|
|
Assuming m_ctrlTaskList is the Listcontrol you use:
POSITION pos = m_ctrlTaskList.GetFirstSelectedItemPosition();
while (pos != NULL)
{
int nPosition = m_ctrlTaskList.GetNextSelectedItemPosition(pos);
m_ctrlTaskList.DeleteItem(nPosition);
}
HTH
--
Alex Marbus
www.marbus.net
But then again, I could be wrong.
|
|
|
|
|
Thats what my code looks like exactly except for a TRACE I did on Position to Check for NULL.
However, I did use your code exactly and my problem still remains. I am only deleteing every other
Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves
246 intact and still selected. Am I a BOOB? This is irking me especially since its one of my last bugs, and I cant implement anything else until this is solved.
Here Ill even copy out my code for you.
I really need help on this.
void CChildView::OnRemItems()
{
POSITION pos = m_SpewWnd.QueueView.GetFirstSelectedItemPosition();
while (pos != NULL)
{
int nItem=m_SpewWnd.QueueView.GetNextSelectedItem(pos);
m_SpewWnd.QueueView.DeleteItem(nItem);
}
Invalidate();
}
Maybe you could also tell me how to get that code selected while posting..
=)
recurse.org - Recusion For the Rest Of Us.
|
|
|
|
|
I am only deleteing every other Item that is selected, SO if I am deleteing item 123456 which are all selected using that code above leaves 246 intact and still selected.
That's because when you delete item 1, the result is that 12345 are selected (2-6 shift down to 1-5), so the next time through your loop you skip the new item 1, item 2 (previously 3) gets deleted, and so on.
Easy way to fix: In your loop, get the first selected item and delete it. Then get the first selected item again and delete it. Repeat until there are no more selected items. This may be slow if your list is big, though.
Snazzier way to fix: Loop thru and make a list of the indexes of all selected items. Then delete them working from the highest index to the lowest.
--Mike--
http://home.inreach.com/mdunn/
All your base are belong to ME~!
|
|
|
|
|
The post Below this describes how to do this Task Recursively, but that has a lot of overhead if your list of selected Items is really large. I figured out the "mIchael Dunn" method sometime this morning and that works very well. Using GetFirstSelected() over and over. The List Method is probably the best way to go however.
Thanks EVERYONE.
recurse.org - Recusion For the Rest Of Us.
|
|
|
|
|
Mike,
Can you give a bit more info regarding the "Snazzier way to fix". I need to implement this and i'm having the same problems.
Thanks,
Frank
|
|
|
|
|
Okay, I got it working, BUt this is how.. good old recursion.
All I did was read to the end of the selected list and then recursively delete from there.. heres the code:
Does anyone have a better IDEA ?
void CChildView::OnRemItems()
{
POSITION pos = m_SpewWnd.QueueView.GetFirstSelectedItemPosition();
DelQ(pos);
Invalidate();
}
void CChildView::DelQ(POSITION pos)
{
while (pos != NULL)
{
int nItem=m_SpewWnd.QueueView.GetNextSelectedItem(pos);
DelQ(pos);
m_SpewWnd.QueueView.DeleteItem(nItem);
}
}
Is this the asinine way to do this? I like recursion But I dont think MS had this in Mind at all.
recurse.org - Recusion For the Rest Of Us.
|
|
|
|
|
i have a wince 2.11 application that needs to detect if it has just come out of hybernation.
i have no idea how to do this and i would really really appreciate any help.
|
|
|
|
|
Well, the only way to this is to write a device-driver...
Win CE does not notify applications when it's powered on/wakes from hybernation, it only notifies drivers.
I did it, for test/fun, last year, if you have too much trouble, mail me at am@nimo.dk and I can send you the project.
Who knows, maybe we could write an article about it...
- Anders
|
|
|
|
|
I want to read my HD's boot sector and dump it to a file on my w2k system, is there a way to do this?
Have a good one,
-Ben
"Its funny when you stop doing things not because they’re wrong, but because you might get caught." - Unknown
|
|
|
|
|
Check out the docs on CreateFile() - you can pass "\\.\C:" as the file name (which would actually be "\\\\.\\C:" in C code) to get a handle which can read sectors of the C: drive. The first sector is the boot sector, so you can use ReadFile() and read 512 bytes to get the contents of the boot sector. This requires admin privs, for obvious reasons.
HANDLE hDevice;
BYTE byBuffer[512];
DWORD dwBytesRead;
hDevice = CreateFile ( _T("\\\\.\\C:", GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING, 0, NULL );
SetFilePointer ( hDevice, 0, 0, FILE_BEGIN );
ReadFile ( hDevice, byBuffer, 512, &dwBytesRead, NULL );
CloseHandle ( hDevice ); You obviously need error handling in there, but I left that out for clarity.
--Mike--
http://home.inreach.com/mdunn/
All your base are belong to ME~!
|
|
|
|
|
Thanks!
Have a good one,
-Ben
"Its funny when you stop doing things not because they’re wrong, but because you might get caught." - Unknown
|
|
|
|
|
CFile and my class
some problem when I use Cfile to write and read my class
I write this class :
class omega
{
public:
CString o_str;
omega();
virtual ~omega();
};
after that (write work) in functio with button control :
void CMyDialog::OnButWrite()
{
char name[]="mydata.dat";
CFile cfl(name,CFile::modeCreate | CFile::modeWrite);
omega om;
om.o_str = "OMEGA";
cfl.Write(&om,sizeof(om));
cfl.Close();
}
then (read work) in functio with button control :
void CMyDialog::OnButRead()
{
char name[]="mydata.dat";
CFile cfl(name,CFile::modeRead);
omega om ;
cfl.Read(&om,sizeof(om));
MessageBox(om.om2.o_str,"DATA FROM mydata.dat " );
}
now the problem here ...
the program was compiled without any errors
but in run time , when you do write work and then read work
error message apeare and tell me that :
that's caused an error in MSVCRTD.DLL
then another message apeare and tell me that :
Debug Assertion Failed!
FILE:dbgheap.c
LINE:1017
Expression : _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
this problem is apeare when I use CString in my calss and didn't
apear when I use base types like char,int...etc
any one can helpe me...
AHMAD ALWASHALI
|
|
|
|
|
Why don't you use a CArchive to read/write from/to your class?
You can easily use WriteString then (be sure to add a newline while writing to a CArchive)
See MSDN Library, topic CArchive (and Serialize) for more info.
--
Alex Marbus
www.marbus.net
But then again, I could be wrong.
|
|
|
|
|
Your problem is that a CString object does not contain the string itself, only a pointer to the string. When you write your omega object to the file, you only write the pointer, which is then invalid when you read the object back in and try to use it.
--Mike--
http://home.inreach.com/mdunn/
All your base are belong to ME~!
|
|
|
|
|
How does MFC create a CHttpConnection object on the calling processes heap
via the CInternetSession::GetHttpConnection method call? I am trying to
accomplish something similar using my own DLL and all of the object I return
get created on the DLL's heap and when the calling process deletes the object
I get access violations. Anyone know how MFC does this?
Thanks
Brad
|
|
|
|
|
A DLL doesn't have a separate heap. The heap belongs to the process, which is your EXE and all DLLs (and other modules like OCXs) that are loaded as well. So just allocate objects with new.
--Mike--
http://home.inreach.com/mdunn/
All your base are belong to ME~!
|
|
|
|
|
I believe you are wrong. I have a very simple DLL with the following two
classes and code:
#ifdef BF_EXPORTS
#define BF_API __declspec(dllexport)
#else
#define BF_API __declspec(dllimport)
#endif
class BF_API CBar {
public:
CBar( void ) {
m_bFlag = true;
}
private:
bool m_bFlag;
};
class BF_API CFoo {
public:
CFoo( void ) {
}
CBar * MakeBar( void ) {
CBar * cResult = new CBar();
return cResult;
}
};
I have a very simple console application that uses these two classes and
then deletes the new'd objects:
#include <stdio.h>
#include "BF.h"
int main(int argc, char* argv[])
{
CFoo * foo = new CFoo;
CBar * bar = foo->MakeBar();
delete bar;
delete foo;
return 0;
}
When built and run Debug, an assert generated in HeapValidate via
_CrtIsValidHeapPointer when the delete of bar is performed.
Brad
|
|
|
|