Check that a buffer has been completely written

Discussion in 'C++' started by Paolo, Jul 6, 2006.

  1. Paolo

    Paolo Guest

    Hi all!

    I have to create an application that receive some packet from an
    interface and builds a file from those. In the header there are three
    fields: one is the total lenght of the file, one is the offset of the
    packet in the file and one is the lenght of the current packet.
    I made a function that gets all the packets and reconstruct the file.

    while(...)
    {
    FilePart part;
    .......
    char* fileBuffer = new char[fileLenght];
    int offset = part->offset;
    int lenght = part->lenght;
    memmove(fileBuffer + offset, part->buffer, lenght);
    lenghtCheck += lenght;
    }

    I used lenghtCheck to check that the sum of all the lenghts is the
    same as the original file. If this is true, I then write the file.
    (I didn't post a complete code since this seems to work)
    My problem is that the file isn't the same as the original opnbe, some
    parts are missing. I though that maybe I overwrite some parts of the
    buffer (maybe some offsets are wrong?).
    I'd like a function to check that every byte of the fileBuffer array
    has been overwritten. DO you know how I can do this? The file is
    binary, not text.

    Thank you very much for your help
    Paolo, Jul 6, 2006
    #1
    1. Advertising

  2. * Paolo:
    >
    > I have to create an application that receive some packet from an
    > interface and builds a file from those. In the header there are three
    > fields: one is the total lenght of the file, one is the offset of the
    > packet in the file and one is the lenght of the current packet.
    > I made a function that gets all the packets and reconstruct the file.
    >
    > while(...)
    > {
    > FilePart part;
    > ......
    > char* fileBuffer = new char[fileLenght];
    > int offset = part->offset;
    > int lenght = part->lenght;
    > memmove(fileBuffer + offset, part->buffer, lenght);
    > lenghtCheck += lenght;
    > }


    'int' may not have the range required to represent a file size.


    > I used lenghtCheck to check that the sum of all the lenghts is the
    > same as the original file. If this is true, I then write the file.
    > (I didn't post a complete code since this seems to work)
    > My problem is that the file isn't the same as the original opnbe, some
    > parts are missing. I though that maybe I overwrite some parts of the
    > buffer (maybe some offsets are wrong?).
    > I'd like a function to check that every byte of the fileBuffer array
    > has been overwritten. DO you know how I can do this?


    For example, use a std::bitset where each bit represents one byte.
    Update the bitset as you write. Check it at the end.


    > The file is binary, not text.


    Irrelevant.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Jul 6, 2006
    #2
    1. Advertising

  3. Paolo

    Paolo Guest

    Well, maybe you'll say that this is not the correct group, but I'm
    using MFC, and I think they won't be happy with std stuff...
    Paolo, Jul 6, 2006
    #3
  4. Paolo schrieb:
    > Well, maybe you'll say that this is not the correct group, but I'm
    > using MFC, and I think they won't be happy with std stuff...


    1) please quote.
    2) Why should "they" mind what you do in your code?
    3) Who is "they"?

    Finally:
    4) Did you open the file as binary in both places? Maybe the
    newline-converter corrupted the file.

    --
    Thomas
    Thomas J. Gritzan, Jul 6, 2006
    #4
  5. Paolo

    Paolo Guest

    Thomas J. Gritzan ha scritto:

    > Paolo schrieb:
    > > Well, maybe you'll say that this is not the correct group, but I'm
    > > using MFC, and I think they won't be happy with std stuff...

    >
    > 1) please quote.
    > 2) Why should "they" mind what you do in your code?
    > 3) Who is "they"?
    >


    Well, I read somewhere that when you use MFC, you get problem using
    standard library containers, such as list and so on. After reading
    this, I also tried it, and I must say that this is true. The problem is
    MFC redefine new operator, and this is a problem for the standard
    library.
    All this to say hat I can't use the container you seggested. Anyway I
    use an array of int where I store the beginning and the end of every
    buffer I write, so I can check if they overlap.

    > Finally:
    > 4) Did you open the file as binary in both places? Maybe the
    > newline-converter corrupted the file.
    >


    Yes I opened the files as binary.
    > --
    > Thomas
    Paolo, Jul 6, 2006
    #5
  6. Paolo

    Jim Langston Guest

    "Paolo" <> wrote in message
    news:...
    > Hi all!
    >
    > I have to create an application that receive some packet from an
    > interface and builds a file from those. In the header there are three
    > fields: one is the total lenght of the file, one is the offset of the
    > packet in the file and one is the lenght of the current packet.
    > I made a function that gets all the packets and reconstruct the file.
    >
    > while(...)
    > {
    > FilePart part;
    > ......
    > char* fileBuffer = new char[fileLenght];
    > int offset = part->offset;
    > int lenght = part->lenght;
    > memmove(fileBuffer + offset, part->buffer, lenght);
    > lenghtCheck += lenght;
    > }
    >
    > I used lenghtCheck to check that the sum of all the lenghts is the
    > same as the original file. If this is true, I then write the file.
    > (I didn't post a complete code since this seems to work)
    > My problem is that the file isn't the same as the original opnbe, some
    > parts are missing. I though that maybe I overwrite some parts of the
    > buffer (maybe some offsets are wrong?).
    > I'd like a function to check that every byte of the fileBuffer array
    > has been overwritten. DO you know how I can do this? The file is
    > binary, not text.
    >
    > Thank you very much for your help


    Without any knowledge of how your part is built, it's hard to say. It may
    be a simple off by one error. Maybe your offset is one too little or one
    two many, ect...

    I would suggest you create a text file that you can examine easily. Maybe
    something as simple as:
    Line 1
    Line 2
    Line 3
    etc...

    then run through your code, write the file, and look at the output.

    Now, you want to know if every byte has been set in your buffer. You can do
    this with math, but if your math is wrong creating the part it may be wrong
    in the examining. Something like this though should do it.

    Untested code.

    static LastByteSet = 0;

    while (...)
    {
    if ( offset != 0 && LastByteSet != offset -1 )
    // Error, missing some bytes here or overwriting
    else
    {
    memmove(fileBuffer + offset, part->buffer, lenght);
    LastByteSet = (offset + length) - 1;
    // Notice -1 here. If your offset was 0 and length was 1, only byte
    0 would be written
    }
    }

    // LastByteSet should be equal to lengthCheck here
    Jim Langston, Jul 7, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad
    Replies:
    8
    Views:
    3,286
    Marian Kostal
    Jan 11, 2007
  2. =?Utf-8?B?Um95?=
    Replies:
    1
    Views:
    2,012
    =?Utf-8?B?Um95?=
    May 2, 2005
  3. Replies:
    0
    Views:
    622
  4. bill.wu
    Replies:
    0
    Views:
    279
    bill.wu
    Jan 17, 2008
  5. justind
    Replies:
    3
    Views:
    268
    justind
    May 11, 2009
Loading...

Share This Page