[MFC] Cum fac download la un fisier text?

Despre MFC, ATL si alte biblioteci C++ de la Microsoft (forum moderat)
Post Reply
mesajflaviu
Membru++
Membru++
Posts: 688
Joined: 10 Sep 2008, 21:40
Judet: Cluj

[MFC] Cum fac download la un fisier text?

Post by mesajflaviu » 31 Jan 2011, 13:31

Intrebare: Cum fac download la un fisier text (TXT, HTML, XML, etc) sau la sursa unei pagini web?

Raspuns: In MFC se poate folosi simplu un pointer tip CStdioFile returnat de CInternetSession::OpenURL.

Exemplu

Code: Select all

void CInternetDoc::DownloadTextFile(LPCTSTR pszAddress, CString& strResult)
{
   strResult.Empty();

   CInternetSession session;
   CStdioFile* pFile = session.OpenURL(pszAddress);
   CString strLine;
   while(pFile->ReadString(strLine))
   {
      strResult += strLine + _T("\r\n");
   }

   if(NULL != pFile)
   {
      pFile->Close();
      delete pFile;
   }
}

Code: Select all

void CInternetDoc::OnDemoDownloadTextFile() 
{
   // just for demo purpose
   CString strAdderss = _T("http://www.codexpert.ro/");

   try
   {
      DownloadTextFile(strAdderss, m_strText);
   }
   catch(CException* pException)
   {
      pException->ReportError();
      pException->Delete();
   }
}
Vezi si << Back to MFC index



Post Reply