C++: Read files in 4096B steps [duplicate]
Asked Answered
C

1

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;
    }
Chipboard answered 12/7, 2014 at 11:24 Comment(8)
You can't fseek on a socket.Huntsman
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.Huntsman
Your question says 4096kB, but your code just reads in 4096b steps.Huntsman
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.Philander
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 endChipboard
@schacker22: Change those delete (buffer); calls to delete [] buffer;. Since you used the array form of new[] you must use the corresponding delete[]Battologize
@Barmar: Yeah sorry, I mean 4096B so 4kB buffer, i've changed itChipboard
One problem I see is using char instead of unsigned char for your buffer.Voroshilovsk
E
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!

Etruria answered 12/7, 2014 at 19:11 Comment(1)
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 againChipboard

© 2022 - 2024 — McMap. All rights reserved.