Thanks for all this usefull/informative information.
data that follows.
Thanks for the help.
"Joseph M. Newcomer" <firstname.lastname@example.org> wrote in message
On Tue, 22 Apr 2008 13:24:32 GMT, "Kahlua" <email@example.com> wrote:
I have a ListBox with a list of files in the c:\data\ folder with the
Now that I have the file coppied into buffer how do I search through the
for a specific sequence of bytes?
at the VERY least. Better still,
nSelect=SendDlgItemMessage(IDC_LIST, LB_GETCURSEL, 0, 0L);
Why such a crude and antiquated mechanism? Create a control variable for
your list and do
nSelect = c_List.GetCurSel();
note how much easier it is!
DlgDirSelect((LPSTR) cSelect, IDC_LIST);
Note that DlgDirSelect makes the GetCurSel superfluous, but actually the
simplest thing to
do is to write
which is a whole lot easier
I can't figure out what this is doing because I have no idea what the
purpose of it is.
For example, what in the world is 0x2e? Perhaps you meant to write
if(cSelect[Length-1] == _T(','))
If you are testing for a character, it is generally considered good
to use the character, and not its hex equivalent.
Also, using the obsolete 'char' data type is not good programming
practice; you should get
the length by writing
Length = _tcslen(cSelect);
But note that this is much more readily written if you have a CString:
if(cSelect.Right(1) == _T("."))
cSelect = cSelect.Left(cSelect.GetLength() - 1);
which is a lot easier to write and understand. Note that you don't need
to get the length
as a separate variable.
JobFile = _T("c:\\data\\");
You are correctly using _T() here, but in a Unicode build the next line
JobFile += cSelect;
JobFile += ".dat";
So why did you use _T() in one literal but not in another?
mess = "Would you like to load ";
mess += cSelect;
mess += " as top ?";
This would be a lot easier to write as
mess.Format(_T("Would you like to load \"%s\" as top ?"), cSelect);
Note that you do not need to declare the variable at the top; you do not
need to declare
it until it is actually needed. Better still, put that string in the
STRINGTABLE and load
it, so you can localize
int a = MessageBox (mess, "Query", MB_ICONINFORMATION|MB_YESNO);
int a = AfxMessageBox(mess, MB_ICONQUESTION | MB_YESNO);
It is NOT an information prompt, it is a question prompt. Use
follows recommended best practice for the caption (uses the program name).
space around binary operators to make them legible
if (a==IDNO) return;
It would be safer to say
if(a != IDYES)
This tests for the actual meaningful value; note the whitespace around the
that it uses two lines, which makes it easier to debug.
DWORD err = ::GetLastError();
msg.Format(_T("Error opening file: %d"), err);
if((INT_PTR)in.Read(buffer.GetData(), buffer.GetSize()) !=
DWORD err = ::GetLastError();
msg.Format(_T("Error reading file: %d"), err);
Are you searching for text or a binary pattern not expressible as text?
If this is text,
and is known to be 8-bit characters, always, one solution is
LPSTR p = buffer.GetBuffer(in.GetLength());
if((INT_PTR)in.Read(p, in.GetLength()) != in.GetLength())
... as above
int n = buffer.Find("abc");
if(n < 0)
If you need to find all instances of an 8-bit character string, you would
have a loop, and
the second parameter of Find would give the starting offset for the next
However, if your file is in UTF-8 encoding, you would have to use the
of the string (the most efficient means) or convert the file to a Unicode
(not efficient for large files, especially if the string ends up not being
your file is potentially Unicode, life gets a good deal more complex, but
I don't want to
get into that here right now.
Joseph M. Newcomer [MVP]
MVP Tips: http://www.flounder.com/mvp_tips.htm