cheng029 发表于 2015-5-28 12:46:11

一个具体的列子:vc操作ftp

  一个ftp操作的例子:
  

Code
1   //自定义的数据类型
2#include
3using namespace std;
4typedef std::list FileList;    //STL的应用
5
6
7FileList myFileList;
8/**//*
9功能:在指定目录下查找指定的文件类型加入队列中
10参数:
111、CString strPath         查找文件路径      IN
122、CString strFileType   查找文件类型      IN
133、FileList &filelst       文件列表          OUT
14*/
15
16bool 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参数:
481、CString strPath         指定文件路径      IN
492、CString strFileName   指定文件名      IN
503、DataReqQueue &m_DataReqQueue文件列表    INOUT
51函数返回值:-1 代表打开文件失败,0代表没有数据,1代表有数据,2代表延迟读取数据
52*/
53
54int 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;jCount;j++)
64            {
65                file.SeekToEnd();
66                file.Write(myFileList->lpData.name,11);
67                file.Write("|",1);
68                file.Write(myFileList->lpData.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参数:
881、CString strPath         指定文件路径      IN
892、CString strFileName   指定文件名      IN
90函数返回值:-1 代表打开文件失败,0代表没有数据,1代表有数据
91*/
92int 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步骤:
1141.在stdafx.h   定义一个消息。如下   #define WM_BASEMSG WM_USER+10
1152.消息的定义:
116如下:
117
118/*
119功能:输出到列表的信息或记录到日志
120参数:
121char *pBuf   输出的内容  INOUT
122*/
123void 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
1363.在你的 youProjAPP.CPP 中添加如下的映射及使用:
137BEGIN_MESSAGE_MAP(CmyssDlg, CDialog)
138    ON_MESSAGE(WM_BASEMSG,OnDisplayPage) //这种映射将使这个消息可以被该函数(OnDisplayPage)使用
139END_MESSAGE_MAP()
140
141*/
1423.显示:
143OnDisplayPage(WPARAM wparam,LPARAM lparam)
144{
145    CString strinfo = (LPCTSTR)wparam; //注意这一句
146   
147    return 0;
148}
149
150/**//*
151功能:FTP登陆功能
152参数:无
153*/
154BOOL 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;
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}
页: [1]
查看完整版本: 一个具体的列子:vc操作ftp