一个ftp操作的例子:
1
<mFileDef.h> //自定义的数据类型2
#include <list>3
using namespace std;4
typedef std::list<CString> FileList; //STL的应用5

6

7
FileList myFileList;8

/**//*9
功能:在指定目录下查找指定的文件类型加入队列中10
参数:11
1、CString strPath 查找文件路径 IN12
2、CString strFileType 查找文件类型 IN13
3、FileList &filelst 文件列表 OUT14
*/15

16
bool CmFileDef::FindDirectoryFile(CString strPath,CString strFileType,FileList &filelst,int nSpanTime) //最后一个参数是设定定时时间加入文件列表17


{18
filelst.clear();19
CFileFind find;20
BOOL bFind = find.FindFile(strPath+strFileType); //这些参数应该通过配置文件获得,一般来说,strFileType是"*.doc" 这样的配置21
CTime currtm,lastwtm; //获取两次写入时间的间隔22
CTimeSpan sptm;23
while(bFind) //如果有文件找到的话24

{25
bFind = find.FindNextFile();26
if ( find.IsDots()||find.IsDirectory()) continue; //记住是文件夹的话返回是"."27
find.GetLastWriteTime(lastwtm);28
currtm = CTime::GetCurrentTime();29
sptm = currtm - lastwtm;30
if(sptm.GetSeconds()>nSpanTime)31

{32
filelst.push_back(find.GetFileName()); //将之插入到文件的队列中33
}34
}35
find.Close(); //这句经常会忘记,不要忘记!36
if(filelst.size()>0)37

{38
filelst.sort();// 会对进入的文件进行排序,如果当文件的操作很频繁的话,是按照时间的顺序来进行排序39
return true;40
}41

42
return false;43
}44

45

/**//*46
功能:读取指定目录下的指定的文件的内容加入队列内存中47
参数:48
1、CString strPath 指定文件路径 IN49
2、CString strFileName 指定文件名 IN50
3、DataReqQueue &m_DataReqQueue 文件列表 INOUT51
函数返回值:-1 代表打开文件失败,0代表没有数据,1代表有数据,2代表延迟读取数据52
*/53

54
int CFileEx::ListWriteToFile(CString strPath,CString strFileName,Query &myFileList)//返回值: -1 代表打开文件失败,0代表没有数据,1代表有数据。 需要//对 //Query是List进行的下一步的包装 55


{56
int nCount = myFileList.GetCount();57
if(nCount>0)58

{59
CStdioFile file;60
if(file.Open(strPath+strFileName,CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate))61

{62
TQueryReq *pQueryReq = m_QueryReqQueue.GetFront(); //文件的头63
for(int j=0;j<myFileList->Count;j++)64

{65
file.SeekToEnd();66
file.Write(myFileList->lpData[j].name,11);67
file.Write("|",1);68
file.Write(myFileList->lpData[j].age,3);69
file.Write("\r\n",2);70
}71
file.Close();72
delete[](myFileList->lpData);73
myFileList->lpData = NULL;74
delete myFileList;75
myFileList= NULL;76
return 1;77
}78
return -1;79
}80

else
{81
return 0;82
}83
}84

85

/**//*86
功能:读取队列内存中数据写到指定目录下的指定的文件87
参数:88
1、CString strPath 指定文件路径 IN89
2、CString strFileName 指定文件名 IN90
函数返回值:-1 代表打开文件失败,0代表没有数据,1代表有数据91
*/92
int CFileEx::WriteLogToFile(CString strPath,char * pBuf)//返回值: -1 代表打开文件失败,1代表有数据93


{94
CTime ct;95
ct = CTime::GetCurrentTime();96
CString strCurrentTime,strCurrentDate;97
strCurrentTime = ct.Format("%Y-%m-%d %H:%M:%S");98
strCurrentDate = ct.Format("%Y-%m-%d");99
CStdioFile file; 100
if(file.Open(strPath+strCurrentDate+".log",CFile::modeCreate|CFile::modeWrite|CFile::modeNoTruncate)) //不存在文件的话就直接创造101

{102
file.SeekToEnd(); //移动到文件的尾部103
file.Write(strCurrentTime.GetBuffer(),strCurrentTime.GetLength());104
file.Write(pBuf,strlen(pBuf)); //写文件105
file.Close();106
return 1;107
}108
return -1;109
}110

111

/**//*112
目的:如何创建自己的消息?113
步骤:114
1.在stdafx.h 定义一个消息。如下 #define WM_BASEMSG WM_USER+10115
2.消息的定义:116
如下:117

118
/* 119
功能:输出到列表的信息或记录到日志120
参数:121
char *pBuf 输出的内容 INOUT122
*/123
void CServerOption::WriteBaseMsg(char *pBuf)124


{125

126
m_FileEx.WriteLogToFile(szLogPath,pBuf);127

/**//*在这个线程函数中可以可以通过设置MT_INTERVAL来控制这个线程的函数体多久执行一次,当事件为无信号状态时函数体隔MT_INTERVAL执行一次,当设置事件为有信号状态时,线程就执行完毕了128
*/129
if(::WaitForSingleObject(m_StopEvent,0)!=WAIT_OBJECT_0)130

{131
::SendMessage(hBaseHwd,WM_BASEMSG,(WPARAM)pBuf,0);132
}133

134
}135

136
3.在你的 youProjAPP.CPP 中添加如下的映射及使用:137
BEGIN_MESSAGE_MAP(CmyssDlg, CDialog)138
ON_MESSAGE(WM_BASEMSG,OnDisplayPage) //这种映射将使这个消息可以被该函数(OnDisplayPage)使用139
END_MESSAGE_MAP()140

141
*/142
3.显示:143
OnDisplayPage(WPARAM wparam,LPARAM lparam)144


{145
CString strinfo = (LPCTSTR)wparam; //注意这一句146

147
return 0;148
}149

150

/**//* 151
功能:FTP登陆功能152
参数:无153
*/154
BOOL CServerOption::FTPLogin()155


{156
::EnterCriticalSection(&m_rCriticalSection); 157
if(m_pInetSession==NULL)158
m_pInetSession=new CInternetSession(AfxGetAppName(),1,PRE_CONFIG_INTERNET_ACCESS);159
else if(m_pFtpConnection!=NULL)160
m_pFtpConnection->Close();161
WORD TimeOut = gSysInfo.nTimeOut; 162
m_pInetSession->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,&TimeOut,sizeof(TimeOut));163
m_pInetSession->SetOption(INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT,&TimeOut,sizeof(TimeOut));164
m_pInetSession->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,&TimeOut,sizeof(TimeOut));165

166
WriteBaseMsg("FTP连接中
,请稍后!\r\n",0);167
try 168

{169
m_pFtpConnection=m_pInetSession->GetFtpConnection(gSysInfo.szIp,gSysInfo.szUser,gSysInfo.szPass,gSysInfo.nPort);170
if (m_pFtpConnection != NULL)171

{ 172
m_pFtpConnection->SetOption(INTERNET_OPTION_CONNECT_TIMEOUT,&TimeOut,sizeof(TimeOut));173
m_pFtpConnection->SetOption(INTERNET_OPTION_CONTROL_RECEIVE_TIMEOUT,&TimeOut,sizeof(TimeOut));174
m_pFtpConnection->SetOption(INTERNET_OPTION_DATA_RECEIVE_TIMEOUT,&TimeOut,sizeof(TimeOut));175
WriteBaseMsg("FTP连接成功\r\n",0);176
::LeaveCriticalSection(&m_rCriticalSection);177
return TRUE;178
}179
}180
catch(CInternetException * pEx)181

{182
CString strErrorMsg;183
strErrorMsg = "FTP连接不成功";184
TCHAR szError[1024];185
if (pEx->GetErrorMessage(szError,1024)) 186

{187
strErrorMsg.Format("FTP连接不成功,错误代码:%s\r\n",szError);188
WriteBaseMsg(strErrorMsg.GetBuffer(),0);189
}190
pEx->Delete();191
m_pFtpConnection=NULL;192
::LeaveCriticalSection(&m_rCriticalSection);193
return FALSE;194
}195
::LeaveCriticalSection(&m_rCriticalSection);196
return true;197
}