Re: BYTE?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Carl Daniel [VC++ MVP] (cpdaniel_remove_this_and_nospam_at_mvps.org.nospam)
Date: 03/25/04


Date: Thu, 25 Mar 2004 07:08:01 -0800

newbie wrote:
> I am downloading get request for files. The files are basically the
> header followed by bytes of data. What is the best way to process the
> file, removing the header?
> I was going to use the stl string class. But this gave me trouble.
> The problem is I could remove the header but the left over data in
> the buffer got currupted when i cast it from string to BSTR.

I don't see any use of BSTR in the code you show here. You certainly can't
cast anything from "char *" to "BSTR" since the former is a "narrow" string
(8-but characters) and the latter is a "wide" string (16-bit characters).

> Should I
> use string classes, like stl string. Any help greatly appreciated.
> Code even more.
>
> I was trying to do something like this

//onread of socket
void onread(char *data)
{
    //when it is true no longer need to look for header
    if(decodeheader == false)
    {
          buff += data;
          int x = buff.find("\r\n\r\n");
          if(x < string::npos)
          {
               int len = buff.length();
/*
               char *ptr = new char[len];
               buff.copy(ptr,len - x,x);
               WriteFile(hfile,(BYTE *) ptr, len - x, &dwBytesWritten,
NULL);
*/
                // you don't need to allocate a new buffer
                // you do need to account for the \r\n\r\n
                // (unless you want those written to the file)
                x += 4;
                WriteFile(hfile,(BYTE*) buff.c_str()+x, len-x,
&dwBytesWritten, NULL);

              decodeheader = true;
              return true;
          }
    }
    else
    {
        WriteFile(hfile,(BYTE*)data, a, &dwBytesWritten, NULL);
    }
}

While I did make a couple of changes, I don't see anything in your original
code that would lead to "corruption". You did have a memory leak, but then
it's also clear that you've left oiut some of the code. If you could show a
more complete example of what you're trying to do, and describe "corrupted"
a bit better, it would improve the odds of someone posting a solution to
your dilema.

-cd



Relevant Pages

  • RE: Chr(0) in .NET causes string to return blank
    ... the string is *not* truncated: ... There no "corruption" going on. ... Fundamentally a string is a sequence of characters, ... other things such as the debugger and message boxes, ...
    (microsoft.public.dotnet.framework)
  • tcl 8.5.9: extension commands binary string result predictably corrupted
    ... binary string, and the pattern of corruption is reliable. ... Certain *sequences* of characters seem to ... The relevant C++ code implementing the Tcl command looks like this: ...
    (comp.lang.tcl)
  • Re: Is it possible for me to have an alert pop-up when I open a do
    ... them and clean up the whole header. ... Dim TheWeekOfStr As String ... After I enabled macros and changed the security level, as per Dave Peterson, ... I got almost what I wanted, except that the pop-up box contains the font ...
    (microsoft.public.excel.misc)
  • Re: Zero terminated strings
    ... For doing major string handling you want a language where strings are a ... Check the header bytes as they are coming in and assume that you have dropped at least one byte if any of them are wrong. ... When you have a complete valid message (at the final destination, however many hops down the line it is) the final destination acknowledges it, and keeps sending ACKs for it until it receives the next message. ... The sender keeps trying to send it until it gets the acknowledgment. ...
    (comp.lang.c)
  • Re: swapping commas for spaces in a string..
    ... don't use a second string ssvstr. ... Valid value, not part of a header, thus backtrack one record ... infile comes from the command line. ...
    (comp.lang.fortran)