0

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;
    }
tshepang
  • 10,772
  • 21
  • 84
  • 127
schacker22
  • 399
  • 2
  • 4
  • 12
  • You can't `fseek` on a socket. – Barmar Jul 12 '14 at 11:30
  • The length argument to `fread()` 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. – Barmar Jul 12 '14 at 11:32
  • 1
    Your question says `4096kB`, but your code just reads in `4096b` steps. – Barmar Jul 12 '14 at 11:34
  • You should not use a string as output, just create a char buffer before the loop and pass a pointer of the buffer to read. – dari Jul 12 '14 at 11:36
  • What do you mean with I can't use fseek on a socket? I'am using fseek to set the position in the file, and in the loop this parameter is counted up by 4096, so i can read only 4096kb of the file, until it's at the end – schacker22 Jul 12 '14 at 11:37
  • 2
    @schacker22: Change those `delete (buffer);` calls to `delete [] buffer;`. Since you used the array form of `new[]` you must use the corresponding `delete[]` – Blastfurnace Jul 12 '14 at 11:53
  • @Barmar: Yeah sorry, I mean 4096B so 4kB buffer, i've changed it – schacker22 Jul 12 '14 at 13:36
  • One problem I see is using `char` instead of `unsigned char` for your buffer. – Cole Johnson Jul 12 '14 at 13:36

1 Answers1

0

You are currently waiting for an ok response every iteration of the while loop.

There are a few other things that you should really look at doing to get a better result:

  1. Open the file you're transferring once, read from it in blocks each iteration, then close it once. Opening and closing it multiple times is not good

  2. As others have mentioned, you have mixed array-new with delete. Use array delete, ie. delete [] buffer;

  3. Don't use strings as your intermediate buffer type!

jtlim
  • 3,001
  • 1
  • 14
  • 14
  • I don't understand 1., I'am not opening and closing the file multiple times, I have a 'Stream.intit ()' function where the file is opened, and a 'Stream.quit ()' where it is closes again – schacker22 Jul 12 '14 at 23:45