?
Solved

CInternetFile ??

Posted on 2000-01-26
4
Medium Priority
?
333 Views
Last Modified: 2013-11-20
HI,
   I have a file in the server, i want to read that file & then append some data to the data of that file & then write this data to that file.
  How can i do it?
  i was using Read & Write functions of this class but its giving some problems to me.
 help.
 
0
Comment
Question by:kulsmadya
  • 2
4 Comments
 
LVL 4

Expert Comment

by:inpras
ID: 2392183
Hi
How UR doing it what kind of site is it?
if U have a ftp site then append is not possible U have to down load it append and upload again some code sure will help us to help U
inpras
0
 

Author Comment

by:kulsmadya
ID: 2392207
i am doing ftp...
 but when i use to read the file , it's giving error..
 i am able to write into file.. but to append , i should be reading the file first & then append & then write the file...
 
0
 
LVL 4

Expert Comment

by:inpras
ID: 2392412
Yes for ftp it won't work copy the file into UR local disk append it and keep it back in Ftp server. thats the only way around
0
 
LVL 1

Accepted Solution

by:
AndersonAdrian earned 100 total points
ID: 2394931
Here's code that will download a file from an FTP site, it should get you started.  Once you have the file, make the changes, then to upload, you do follow the same steps as downloading except call CFtpConnection::PutFile(LPCTSTR pstrLocalFile, LPCTSTR pstrRemoteFile, DWORD dwFlags = FTP_TRANSFER_TYPE_BINARY, DWORD dwContext = 1 );

--Good Luck


CInternetSession session;
CFtpConnection* pServer = NULL;
CInternetFile* pFile = NULL;

char sFile[] = "ftp://ftp.foobar.com/pub/file.exe";
char* sPath;

try
{
// check to see if the URL is reasonable

      CString strServerName;
      CString strObject;
      CString strDir;
      CString strFile;
      int nPos;
      INTERNET_PORT nPort;
      DWORD dwServiceType;

      if (!AfxParseURL(sFile, dwServiceType, strServerName, strObject, nPort) ||
            dwServiceType != INTERNET_SERVICE_FTP)
      {
            ASSERT(0); //bad URL                  
            return NULL;
      }
                                                                                                
      pServer = session.GetFtpConnection(strServerName, NAME, PASSWORD, nPort);

      nPos = strObject.ReverseFind('/');
      strDir = strObject.Left(nPos);
      strFile = strObject.Right(strObject.GetLength() - nPos -1);

      strDir.Delete(0);

      pServer->SetCurrentDirectory(strDir);
      CFtpFileFind ftpFileFind(pServer);

      if(ftpFileFind.FindFile(strFile))
      {
            pFile = pServer->OpenFile(strFile);
      }


      CFile file;
      char* sTmp;

      sTmp = (char*)malloc(_MAX_PATH);
      sPath = (char*)malloc(_MAX_PATH);

      GetTempPath( _MAX_PATH, sTmp);

      GetTempFileName(sTmp, "~xxx~", 0, sPath);

      file.Open(sPath, CFile::modeReadWrite);


      TCHAR sz[1024];
      int nCount = 0;
      do
      {
            nCount = pFile->Read(sz, 1023);
            file.Write(sz, nCount);
      }
      while (nCount > 0);

      file.Flush();
      file.Close();


      pFile->Close();
      pServer->Close();
}
catch (CInternetException* pEx)
{
      // catch errors from WinINet
      TCHAR szErr[1024];
      pEx->GetErrorMessage(szErr, 1024);

      ASSERT(0);
      pEx->Delete();
}

if (pFile != NULL)
{
      delete pFile;
}

if (pServer != NULL)
{
      delete pServer;
}

session.Close();

return sPath;
}
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Suggested Courses
Course of the Month3 days, 21 hours left to enroll

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question