If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
There are streams which do not have size by definition, such as socket streams. In that cases, GetSize returns 0 so you should always test its return value."
You seem to be dealing with a socket stream.
Compilers: gcc-3.3.6, gcc-3.4.5, gcc-4.0.2, gcc-4.1.0 and MSVC6
OS's: Gentoo Linux, WinXP; WX: CVS HEAD
Returns the file size in bytes or -1 if the file doesn't exist or the size couldn't be determined. Notice that this size can be approximative size only and shouldn't be used for allocating the buffer in which the remote file is copied, for example.
OS: OpenSuSE, Ubuntu, Win XP Pro
wx: svn
Compiler: gcc 4.5.1, VC 2008, eVC 4
"If it was hard to write it should be hard to read..." - the unknown coder
"Try not! Do. Or do not. There is no try." - Yoda
I've been trying to get wxFTP to work so I can download files from an FTP server but I'm running into the same problem, and searching this board and the docs is not giving me a fast solution.
Can wxFTP::GetFileSize() or wxInputStream::GetSize() be fixed?
I even tried sending the FTP "SIZE" command to the server (via wxFTP::SendCommand) but it returns the same result. For example: I'm testing the download of a 227k file and by using a wxInputStream::GetC loop (as shown in several other posts) I get the file but it takes about 10 seconds. If I force the size for wxInputStream::Read to the file's actual size the file downloads almost instantly. Since the size of the file downloads are dynamic is there any way to test for the size successfully? Using GetC() is so slow and downloading 100MB of data is going to be like dial-up again
instead of using GetC() to read single bytes, use wxInputStream::Read(void *buffer, size_t size) to load data in a big buffer and wxInputStream::LastRead() or wxInputStream::Eof() to check if the transfer is finished.
With FTP or HTTP downloads you can never guarantee to know the filesize in advance.
Okay, I've gotten it that far and have the data in the buffer but can't save it to disk. I've also tried displaying the buffer to the screen but haven't had luck there either. I think because it reads the size wrong when it loads the buffer it has no data, but I can't verify that. Can you load the buffer without specifying a size? Just go until EOF?
How do I take the data in the buffer and save it to a file of the same name that was downloaded? Attempts with wxFFile::Write and wxFile::Write get me as far as an empty file of that name. I know I'm missing something somewhere but can't find it. Any direction would be greatly appreciated.
here's a small code-snippet, it downloads a page using wxURL, but the wxInputStream part is the same as with wxFTP, so i guess you can use it for a start:
I played around with that code but it locked up my machine. Best guess is that the last read of the buffer did not fill it, so when it went to write it just hung up. That's the problem I've been having.
So, I tried this variant, mostly from other posts...
wxInputStream* ftpIn = ftp.GetInputStream(filename);
if (ftpIn)
{
file.Open(filename,wxFile::write);
// this code works but is SLOW!
wxString buffer;
wxString oneChar;
oneChar = ftpIn->GetC();
buffer += oneChar;
while ( (ftpIn->LastRead()) > 0 )
{
oneChar = ftpIn->GetC();
buffer += oneChar;
};
file.Write(buffer, buffer.size() );
file.Close();
}
This way the size for the write is correct and the file is saved. It takes roughly 10 seconds for the 222kb file to be pulled from an FTP server running on my own machine (127.0.0.1) one character at a time. Using FTP from the command prompt is an immediate transfer. I was hoping someone had solved dynamically getting the filesize using GetSize() or some other creative means.
Everything appears to hinge on giving a valid size for the Read(). A size larger than the file seems to cause it to hang also. I'll have to try that again to be sure.