2015-01-17 20:00:57|?次阅读|上传:wustguangh【已有?条评论】发表评论
关键词:C/C++, 网络通信|来源:唯设编程网
//得到当前FTP目录 int CFTP::ftp_pwd( char* buff ) { sprintf_s(m_cmd,"PWD "); int err = ftp_sendcmd(m_cmd,m_resp,sizeof(m_resp)); if(err) return -1; //得到返回码 int code = get_state_code(m_resp); //257 "/" is current directory. /* 550-The system cannot find the file specified. Win32 error: The system cannot find the file specified. Error details: File system returned an error. 550 End */ if(code != 257) return -1; char* p=m_resp; while(*p) { if(*p == '"'){ ++p; while(*p != '"') *buff++=*p++; } ++p; } *buff=0; printf("current work directory is : %s ",buff); return 0; }
得到当前FTP目录对应的命令是PWD。
//创建单个文件夹 int CFTP::ftp_mkdirSingle( const char* dir ) { sprintf_s(m_cmd,"MKD %s ",dir); int err = ftp_sendcmd(m_cmd,m_resp,sizeof(m_resp)); if(err) return -1; //得到返回码 int code = get_state_code(m_resp); //257 "20150110" directory created. if(code != 257) return -1; return 0; }
创建目录使用MKD实现,但需要保证父级目录已经存在,如果想递归创建目录,可以使用下面的方法。
//递归创建FTP目录 int CFTP::ftp_mkdir( const char* dir ) { char path[300]={0}; int err = -1; //进入根目录 err = ftp_cd("/"); if(err) return -1; size_t i,j; // printf("strlen(dir):%d",strlen(dir)); for(i=1,j=0;i<strlen(dir);i++) //第一个字节是根目录 { path[j++] = dir[i]; if(dir[i]=='/'){ path[j++]='