CInternetFile ??

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.
 
kulsmadyaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

inprasCommented:
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
kulsmadyaAuthor Commented:
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
inprasCommented:
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
AndersonAdrianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.