473,786 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Check that a buffer has been completely written

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(fileBuf fer + 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

Jul 6 '06 #1
5 1559
* 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(fileBuf fer + 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?
Jul 6 '06 #2
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...

Jul 6 '06 #3
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
Jul 6 '06 #4
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
Jul 6 '06 #5
"Paolo" <pa****@gmail.c omwrote in message
news:11******** *************@p 79g2000cwp.goog legroups.com...
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(fileBuf fer + 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(fileBuf fer + 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
Jul 7 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2225
by: manders2k | last post by:
Hi all -- I'm contemplating the idea of writing a simple emacs-like editor in python (for fun and the experience of doing so). In reading through Craig Finseth's "The Craft of Text Editing": http://www.finseth.com/~fin/craft/ , I've come across the "buffer gap" representation for the text data of the buffer. Very briefly, it keeps the unallocated memory of the
1
6252
by: inkapyrite | last post by:
Hi all. I'm using ifstream to read from a named pipe but i've encountered an annoying problem. For some reason, the program blocks on reading an ifstream's internal buffer that's only half-filled. Only when the buffer becomes full does it resume execution. Here's my test code for reading from a pipe: //(compiled with g++ -std=c++98) //--------------------------------------------- #include <iostream>
9
2880
by: Jon LaBadie | last post by:
Suppose I'm using stdio calls to write to a disk file. One possible error condition is no space on file system or even (in unix environment) a ulimit of 0 bytes. Which calls would be expected to return error codes for such conditions? Would it happen on the writes (fwrite, fprintf, fputs ...), or as they actually write to io buffers, might the errors not occur until the data is flushed or the file is closed?
15
1520
by: Dan DeConinck | last post by:
Hello, I want to save the position of all the controls on my form. I would like to write them out to a preference file. I have done this in DOS BASIC like this: TO WRITE the preference open "c:\preferences.txt" for input as #1 input #1 ,a,b,c
2
3082
by: rdemyan via AccessMonster.com | last post by:
I currently have code that brings up a form where the user can see a list of spreadsheets on my ftp site, select one and then download and import the spreadsheet into a table. All of this processing is initiated by the user. I'm thinking about automating some or all of this. My questions is: how do I best determine when there is an update to a table in my Access Application that is available on my ftp site?
7
20014
by: toton | last post by:
Hi, I want a circular buffer or queue like container (queue with array implementation). Moreover I want random access over the elements. And addition at tail and remove from head need to be low cost. STL vector is suitable for removing form tail? or it is as costly as removing from middle? any other std container to serve this purpose? (note , I dont need linked list implementation of any container, as I want random access)
5
2868
by: arnuld | last post by:
this is from mentioned section. i did not understand some things here: it means "flushing the buffer" and "writing to output device" are SAME thing, these are just 2 different names for the same thing. ?
331
14987
by: Xah Lee | last post by:
http://xahlee.org/emacs/modernization.html ] The Modernization of Emacs ---------------------------------------- THE PROBLEM Emacs is a great editor. It is perhaps the most powerful and most versatile text editor. And, besides text editing, it also serves as a
64
9765
by: Philip Potter | last post by:
Hello clc, I have a buffer in a program which I write to. The buffer has write-only, unsigned-char-at-a-time access, and the amount of space required isn't known a priori. Therefore I want the buffer to dynamically grow using realloc(). A comment by Richard Heathfield in a thread here suggested that a good algorithm for this is to use realloc() to double the size of the buffer, but if realloc() fails request smaller size increments...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.