[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

How can I use OpenFile function in CFtpConnection class???

Hi !

  I want to open a file in the server and append to it .I used OpenFile fn for CFtpConnection object and successfully wrote to it.But I want to append to it ?I read seek fn doesn't work for CFtp objects?How can i do that then ???

   pls help?

Thanks,
ceena.
0
ceena
Asked:
ceena
  • 2
1 Solution
 
waseemanisCommented:
There doesn't seem to be any inbuilt stuff for that , but you can copy the file locally, and then append to it, and put it back on the server. This obviously, the longer way out....but for now, seems to be the only one !!

Luck,
Waseem.
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
 
waseemanisCommented:
AndersonAdrian,
I think if the guy has already gone to the extent of opening a remote file and written to it, then he sure must be knowing how to download and upload it !!

So is it really the answer !!???!!
0

Featured Post

Upgrade your Question Security!

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

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now