Hi all,

I am here with a problem. I am reading a file from Internet. For Example index page of "www.google.com". Once i read it I must write it into a file & I'll use this later.

The problem is that The read file is written completely into file. I didn't understood why?
Can anyone tell the reason.

I have written the code also.

// Allocate a block of memory for lpHeadersW.
lpBufferA = new CHAR [dwSize];
dwSize = MAX_PATH;
HANDLE hFile = CreateFile(_T("\\My Documents\\Config.txt"), GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwBytesRead;
  do
 {
    if (!InternetReadFile (hUrlRequest, (LPVOID)lpBufferA, dwSize, &dwBytesRead))
       {
	//Error in InternetReadFile
        }
     else
       {
	if (dwBytesRead != 0)    
	{
	  lpBufferA [dwSize] = '\0';  // Terminate headers with null.		
                 // Get the required size of the buffer which receives the Unicode String
	  dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0);
	 lpBufferW = new TCHAR [dwSize];// Allocate a block of memory for lpBufferW.
	  MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);  		  WriteFile(hFile, lpBufferW,dwSize, &dwBytesRead, NULL); 
	    delete[] lpBufferW;// Free the block of memory.
	}    
           }
  } while (dwBytesRead);
 CloseHandle(hFile);

//Close all Internet Handle

>>lpBufferA [dwSize] = '\0'; // Terminate headers with null.
Error probably here. The value of dwSize is still MAX_PATH at that point.

Hi..
No this is not the error. Because using the same variable i am writing the contents into log file. In log file it was wriiten properly.


Thanks.

The problem is that The read file is written completely into file.

The code seems to try to do just that, i.e. as long as data is successfully read, write the data. Maybe you need to add some code that breaks the loop based on some condition?

Or did you perhaps intend to say:

The problem is that The read file is NOT written completely into file.

The code seems to try to do just that, i.e. as long as data is successfully read, write the data. Maybe you need to add some code that breaks the loop based on some condition?

Or did you perhaps intend to say:

The problem is that The read file is NOT written completely into file.

Sorry...........it must be the read file is NOT completely written into file.

lpBufferA = new CHAR [dwSize];
dwSize = MAX_PATH;

Those two lines are backwards -- dwSize needs to be set before using it in the allocation line.


>> lpBufferA [dwSize] = '\0';
After calling InternedReadFile() the variable dwBytesRead contains the number of characters read, so the above should probably be lpBufferA[dwBytesRead ]= '\0'; >>MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW, dwSize);
>> WriteFile(hFile, lpBufferW,dwSize, &dwBytesRead, NULL);

Those two functions are also wrong. The dwsize parameter should be dwBytesRead, which is the number of bytes read by InternetReadFile().

Hi,

this problem is solved..

The code which i used is as follows

//Add this header files
#include <iostream>
#include <fstream>
using namespace std;

ofstream ofExfile;
ofExfile.open("config.html",ios::out);

do{
        if(ofExfile.is_open())
        {
             ofExfile << lpBufferA;
         }
}while(cond);

ofExfile.Close();

Not with the code you posted it isn't solved.

I didn't get you.

The content what i read from InternetReadFile API were written into the file using same code what i mentioned in the last post.

If there is mistake in that code then pls tell what it is.

If required I can write whole function here.

Here is complete code.
LOG is a user defined function & nothing todo with the actual code.

void CUrlImport::OnBtnOk()
{
BOOL bReturn = TRUE;
HINTERNET hOpen = NULL, hConnect = NULL, hRequest = NULL;
DWORD dwSize = 0, dwNumberOfBytesToRead ,dwNumberOfBytesRead,
dwFlags = INTERNET_FLAG_RELOAD | INTERNET_FLAG_NO_CACHE_WRITE; 
char *lpBufferA;
TCHAR *lpBufferW;

LPTSTR AcceptTypes[2] = {TEXT("*/*"), NULL}; 
CString csUrl;
m_edtUrl.GetWindowTextW(csUrl);

// Initialize the use of the Windows CE Internet functions.
hOpen = InternetOpen (TEXT("CeHttp"), INTERNET_OPEN_TYPE_DIRECT, NULL, 0, 0);
if(!hOpen)
{
LOG(_T("[Import URL]-Error in Internet Open"));
}
else
{
hConnect = InternetConnect (hOpen, csUrl,INTERNET_INVALID_PORT_NUMBER, NULL,NULL, INTERNET_SERVICE_HTTP, 0, 0);
 if (hConnect == NULL)
{
LOG(_T("[Import URL]-Error in Internet Connect"));
}
else
{      // Open an HTTP request handle. 
if (!(hRequest = HttpOpenRequest (hConnect, TEXT("GET"), NULL, HTTP_VERSION, NULL, (LPCTSTR*)AcceptTypes, dwFlags, 0)))
{
LOG(_T("[Import URL]-Error in HttpOpenRequest"));
bReturn = FALSE;
}
}

if(bReturn)
{
if(!HttpSendRequest(hRequest,NULL,0,NULL,0))
{
LOG(_T("[Import Url]-Error in HttpSendRequest"));
}
else
{
// Call HttpQueryInfoAto find out the size of the headers.
HttpQueryInfoA(hRequest, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwSize,NULL);
lpBufferA = new CHAR [32000];
dwNumberOfBytesToRead = MAX_PATH;
				
ofstream ofExfile;
ofExfile.open("config.xml",ios::out);
do 
{
if (!InternetReadFile (hRequest, (LPVOID)lpBufferA, dwNumberOfBytesToRead, &dwNumberOfBytesRead))
{
LOG(_T("[Import Url]-Error in InternetReadFile"));
}
if (dwNumberOfBytesRead != 0)    
{ 
// Terminate headers with NULL.
lpBufferA [dwNumberOfBytesToRead] = '\0';                 
// Get the required size of the buffer which receives the Unicode string. 
dwSize = MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, NULL, 0); 				      
// Allocate a block of memory for lpBufferW.
lpBufferW = new TCHAR [dwNumberOfBytesRead];
// Convert the buffer from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, lpBufferA, -1, lpBufferW,dwNumberOfBytesRead);  
CString csMsg;
csMsg.Format(_T("\n%s"),lpBufferW);
LOG(csMsg);
if(ofExfile.is_open())
{
ofExfile << lpBufferA;
}
// Free the block of memory.
delete[] lpBufferW;  
  }    
 } while (dwNumberOfBytesRead);

          // Free the block of memory.
         delete[] lpBufferA;  
         ofExfile.close();
         // Close the Internet handles.
         if (hOpen)
        {
                InternetCloseHandle (hOpen);
        }
				  
       if (hConnect)
       {
                InternetCloseHandle (hConnect);
       }

       if (hRequest)
         {
	InternetCloseHandle (hRequest);
         }
      }
    }
  }
  DestroyWindow();
}

Hi,

this problem is solved..

The code which i used is as follows

//Add this header files
#include <iostream>
#include <fstream>
using namespace std;

ofstream ofExfile;
ofExfile.open("config.html",ios::out);

do{
        if(ofExfile.is_open())
        {
             ofExfile << lpBufferA;
         }
}while(cond);

ofExfile.Close();

I didn't get you.

The content what i read from InternetReadFile API were written into the file using same code what i mentioned in the last post.

If there is mistake in that code then pls tell what it is.

If required I can write whole function here.

The solution you posted was not a solution at all but some attempt to simplify the code for us. You should have stated that instead of calling it "a solution". AFAIK the last code you posted is probably correct

This article has been dead for over six months. Start a new discussion instead.