I want to write a ftp class with sockets, textfiles work pretty well to upload so far, but then I wanted to upload a bigger file, a movie, and it didn't work.
The first 4096B are read well, but then it reads just nothing more.
Maybe i'am using the wrong functions, please help my with my code.
Here's my read function:
bool CStream::Read (string * _OutString, unsigned int _iStartPos, unsigned int _iLength)
{
if (!bInitialized)
return false;
int iReturn = 0;
char * buffer;
fseek (pFile, _iStartPos, SEEK_SET);
buffer = new char[(unsigned int) _iLength + 1];
iReturn = fread (buffer, 1, (unsigned int) _iLength, pFile);
if (iReturn == 0)
{
delete (buffer);
return false;
}
else
buffer[iReturn] = '\0';
*_OutString = string (buffer, iReturn);
delete (buffer);
return true;
}
and that's how I call it:
Stream.Read
is the function on top
Stream.FileSize (&iFileSize);
while (iPos < iFileSize)
{
Stream.Read (&ReadData, iPos, 4096);
Socket.Send (ReadData, NULL, NULL);
Socket.Reciev (&RecievData, NULL, NULL);
if (RecievData != "ok")
goto Error;
iPos += 4096;
}
fseek
on a socket. – Huntsmanfread()
is just a limit, there's no guarantee that it will read that much. You need to keep calling in a loop to get all that you want. – Huntsman4096kB
, but your code just reads in4096b
steps. – Huntsmandelete (buffer);
calls todelete [] buffer;
. Since you used the array form ofnew[]
you must use the correspondingdelete[]
– Battologizechar
instead ofunsigned char
for your buffer. – Voroshilovsk