CInternetFile: problem in reading remote file
-
Code ia attached in the reply to the reply below hi, i want to read a file for an ftp application from a remote location. so i used the CInternetFile::Read method to do so. but if i try to read more than 1 byte (say 512 bytes in one read ) then i get the number of bytes read through the return value as only. but the buffer in which i read the contents contains the numbe rof bytes specified and also contains some extra characters. eg:- i have a text file having only 42 characters when i read it by passing 50 as the number of bytes to be read to the Read method then the return value is 1 but the buffer contains the 42 bytes plus some extra characters. how can i solve this problem. Also i want to know the size of the file being copied . is there any function for the filesize. i tried using seekToEnd but it gave me an exception. aditya :)
-
Code ia attached in the reply to the reply below hi, i want to read a file for an ftp application from a remote location. so i used the CInternetFile::Read method to do so. but if i try to read more than 1 byte (say 512 bytes in one read ) then i get the number of bytes read through the return value as only. but the buffer in which i read the contents contains the numbe rof bytes specified and also contains some extra characters. eg:- i have a text file having only 42 characters when i read it by passing 50 as the number of bytes to be read to the Read method then the return value is 1 but the buffer contains the 42 bytes plus some extra characters. how can i solve this problem. Also i want to know the size of the file being copied . is there any function for the filesize. i tried using seekToEnd but it gave me an exception. aditya :)
Post some code... -- jlr http://jlamas.blogspot.com/[^]
-
Post some code... -- jlr http://jlamas.blogspot.com/[^]
hi this is the codewhich i have written for the Download button handler void CSmartFTP2View::OnDownload() { // TODO: Add your control notification handler code here int i = m_list.GetCurSel( ) ; // get selection from list box CString srcfile; m_list.GetText ( i, srcfile ) ; CString str; CInternetFile * pfile; CFile fp; localtarget = localpath; localtarget+= srcfile; srcfile = remotepath + srcfile; srcfile+='\0'; localtarget+='\0'; try { pfile = m_pftp->OpenFile(srcfile.operator LPCTSTR(), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 1); fp.Open(localtarget, CFile::modeCreate |CFile::modeReadWrite); long size = pfile->Seek(0, CFile::end); int pos=0, writebytes=0; while( writebytes = pfile -> Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) { fp.Write ( str, writebytes) ; } MessageBox("Download Successful"); } catch ( CInternetException * pex ) { MessageBox ( "Error in file copy" ) ; pex -> Delete() ; } pfile -> Close( ) ; fp.Close( ) ; } the localtarget will contain the local path and the srcfile has the remote path eg:- ftp://homepc/test.txt aditya
-
hi this is the codewhich i have written for the Download button handler void CSmartFTP2View::OnDownload() { // TODO: Add your control notification handler code here int i = m_list.GetCurSel( ) ; // get selection from list box CString srcfile; m_list.GetText ( i, srcfile ) ; CString str; CInternetFile * pfile; CFile fp; localtarget = localpath; localtarget+= srcfile; srcfile = remotepath + srcfile; srcfile+='\0'; localtarget+='\0'; try { pfile = m_pftp->OpenFile(srcfile.operator LPCTSTR(), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 1); fp.Open(localtarget, CFile::modeCreate |CFile::modeReadWrite); long size = pfile->Seek(0, CFile::end); int pos=0, writebytes=0; while( writebytes = pfile -> Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) { fp.Write ( str, writebytes) ; } MessageBox("Download Successful"); } catch ( CInternetException * pex ) { MessageBox ( "Error in file copy" ) ; pex -> Delete() ; } pfile -> Close( ) ; fp.Close( ) ; } the localtarget will contain the local path and the srcfile has the remote path eg:- ftp://homepc/test.txt aditya
Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) (void *)str.operator LPCTSTR() this has to be a buffer,whose size is 50, which stores the the contents of the bytes read pls make it TCHAR buff[50]; Read ( (LPVOID)buf, 50 )!=0 ) pfile = m_pftp->OpenFile(srcfile.operator LPCTSTR(), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 1); //No need to write it like this srcfile.operator LPCTSTR() (LPCTSTR)srcfile it will return back the pointer of the null-terminated C string contained in a CString object
-
hi this is the codewhich i have written for the Download button handler void CSmartFTP2View::OnDownload() { // TODO: Add your control notification handler code here int i = m_list.GetCurSel( ) ; // get selection from list box CString srcfile; m_list.GetText ( i, srcfile ) ; CString str; CInternetFile * pfile; CFile fp; localtarget = localpath; localtarget+= srcfile; srcfile = remotepath + srcfile; srcfile+='\0'; localtarget+='\0'; try { pfile = m_pftp->OpenFile(srcfile.operator LPCTSTR(), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 1); fp.Open(localtarget, CFile::modeCreate |CFile::modeReadWrite); long size = pfile->Seek(0, CFile::end); int pos=0, writebytes=0; while( writebytes = pfile -> Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) { fp.Write ( str, writebytes) ; } MessageBox("Download Successful"); } catch ( CInternetException * pex ) { MessageBox ( "Error in file copy" ) ; pex -> Delete() ; } pfile -> Close( ) ; fp.Close( ) ; } the localtarget will contain the local path and the srcfile has the remote path eg:- ftp://homepc/test.txt aditya
Aditya Rao wrote: CString str; . . . while( writebytes = pfile -> Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) Besides what sunit5 already told you about the way of using the operator (i.e.: you could just write
(LPCTSTR)str
instead ofstr.operator LPCTSTR()
) I see a far worse problem in your code. That operator returns a constant pointer to the internal buffer of the CString object. It stops being constant because you then cast it to a (void*), but that's not the problem, either. The first problem is that you don't know the size of that buffer, and can't be sure it has enough space to hold the 50 chars you are asking to put there. You might be causing a buffer overrun with code like that. For a buffer size as small of 50, you can just allocate it as follows: const int nBufferSize = 50; BYTE buffer[nBufferSize]; The second problem is the way you wrote the loop condition. The count of bytes read is being compared to 0, and the result of that comparison is then assigned to writebytes. I'm sure that's not what you meant to write. You need to add some parenthesis to change the order of evaluation. So, try changing the loop to the following and see if that helps,const int nBufferSize = 50;
BYTE buffer[nBufferSize];while ((writebytes = pfile->Read((void *)buffer, nBufferSize)) != 0)
{
fp.Write(buffer, writebytes);
}Hope that helps, On another note, please send your response, if any, posting again to the forum, and not by mail. -- jlr http://jlamas.blogspot.com/[^]
-
Aditya Rao wrote: CString str; . . . while( writebytes = pfile -> Read ( (void *)str.operator LPCTSTR(), 50 )!=0 ) Besides what sunit5 already told you about the way of using the operator (i.e.: you could just write
(LPCTSTR)str
instead ofstr.operator LPCTSTR()
) I see a far worse problem in your code. That operator returns a constant pointer to the internal buffer of the CString object. It stops being constant because you then cast it to a (void*), but that's not the problem, either. The first problem is that you don't know the size of that buffer, and can't be sure it has enough space to hold the 50 chars you are asking to put there. You might be causing a buffer overrun with code like that. For a buffer size as small of 50, you can just allocate it as follows: const int nBufferSize = 50; BYTE buffer[nBufferSize]; The second problem is the way you wrote the loop condition. The count of bytes read is being compared to 0, and the result of that comparison is then assigned to writebytes. I'm sure that's not what you meant to write. You need to add some parenthesis to change the order of evaluation. So, try changing the loop to the following and see if that helps,const int nBufferSize = 50;
BYTE buffer[nBufferSize];while ((writebytes = pfile->Read((void *)buffer, nBufferSize)) != 0)
{
fp.Write(buffer, writebytes);
}Hope that helps, On another note, please send your response, if any, posting again to the forum, and not by mail. -- jlr http://jlamas.blogspot.com/[^]
hi , thanks jose . what you said was write, the result of the comparison was being assigned to the writebytes. i changed that and it is working fine. i removed the operator as sunit5 had told and now everything is working fine. in same application i want to add drag and drop from the remote location to the local directories. for this i am using two list controls ( one for each). can you tell how to list the contents of the drives in the list controls ( for the local ones). and i am not able to use the combo box in my application ( my application is a SDI application with the view class derived from CFormView. Is that a constraint for using combo box. i get an exception everytime i am trying to add something in the combo box) . thanks aditya
-
hi , thanks jose . what you said was write, the result of the comparison was being assigned to the writebytes. i changed that and it is working fine. i removed the operator as sunit5 had told and now everything is working fine. in same application i want to add drag and drop from the remote location to the local directories. for this i am using two list controls ( one for each). can you tell how to list the contents of the drives in the list controls ( for the local ones). and i am not able to use the combo box in my application ( my application is a SDI application with the view class derived from CFormView. Is that a constraint for using combo box. i get an exception everytime i am trying to add something in the combo box) . thanks aditya
Aditya Rao wrote: can you tell how to list the contents of the drives in the list controls ( for the local ones). To obtain the files in a given directory, you can use FindFirstFile[^], FindNextFile[^], and FindClose[^]. For each file you find you can add an item to the list. Aditya Rao wrote: and i am not able to use the combo box in my application ( my application is a SDI application with the view class derived from CFormView. Is that a constraint for using combo box. i get an exception everytime i am trying to add something in the combo box I don't think it has anything to do with your application being SDI or your view being a CFormView. Rather, the problem may be at what point in time you are trying to access the combobox. I seem to remember reading in some other post that you are trying to add an item to the combobox from your view's
OnCreate
function. At that point, the combobox is most likely not already created, and thus you won't be able to add any item. If that's the case, try accessing the combobox from your view'sOnInitialUpdate
, after calling the base class implementation. -- jlr http://jlamas.blogspot.com/[^]