I’m trying to split up data into little packets. I’m not exactly sure how this read method is suppose to work but I’ve given a buffer size of 512 to read from the file.
But instead of getting 512 i just get 5 in my first packet. Others vary from 0 to above 512 ( which shouldn’t happen).
It’s a zip file I’m trying to split up:
In text the first few bytes look like this

(de bucket like characters are actually 2 characters)
It seems to grab the first 5 bytes as it should but afterwards just stops and goes to the next read block.
Since it’s a buffer of 512 everything after the first 5 bytes is garbage.
I’m using an ifstream. And the mode is set to Binary.
Any suggestions?
void FileProcessor::send()
{
//If no file is opened return
if(!_file.is_open()) return;
//Reset position to beginning
_file.seekg(0, ios::beg);
//Result buffer
char * buffer;
char * partBytes = new char[_bufferSize];
//Read the file and send it over the network
while (_file.read(partBytes, _bufferSize))
{
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
//cout << strlen(partBytes);
//p->PrintHex(buffer,_bufferSize+Packet::HeaderSize);
//break;
cout << "Normal size : \t" << strlen(partBytes)<< "\tPacketSize: \t" << p->getLength()<<"\n";
//cout << strcmp(p->getData().c_str(),partBytes) << "\n";
writeToFile(p->getData().c_str(),p->getData().length());
delete p;
}
//Write final bytes if any
if(_file.gcount())
{
//writeToFile(partBytes, _file.gcount());
buffer = Packet::create(Packet::FILE,partBytes);
Packet *p = Packet::create(buffer);
writeToFile(p->getData().c_str(),p->getData().length());
//cout << p->getLength() << "\n";
delete p;
}
//cout<< *p << "\n";
delete [] partBytes;
}
Im just testing a direct read write right now.
Inside your loop, instead of assuming it always reads a full buffer of data, use
gcount()to find how many it actually read, and transmit that many.I feel obliged to add that this:
looks quite strange to me. Not sure it’s wrong, but it’s not immediately obvious that it’s right either (and if it is right, the design seems a bit odd).
I’d also skip the dynamic allocation and deletion:
and use a
std::vector<char>instead.