一个ftp操作的例子:

Code
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 查找文件路径 IN
12
2、CString strFileType 查找文件类型 IN
13
3、FileList &filelst 文件列表 OUT
14
*/
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 指定文件路径 IN
49
2、CString strFileName 指定文件名 IN
50
3、DataReqQueue &m_DataReqQueue 文件列表 INOUT
51
函数返回值:-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 指定文件路径 IN
89
2、CString strFileName 指定文件名 IN
90
函数返回值:-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+10
115
2.消息的定义:
116
如下:
117
118
/*
119
功能:输出到列表的信息或记录到日志
120
参数:
121
char *pBuf 输出的内容 INOUT
122
*/
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
}
posted on
2009-04-02 10:41
蚂蚁跳楼
阅读(
681)
评论()
收藏
举报