如何判断文件夹路径合法性

文章正文
发布时间:2025-07-20 17:07

#include "vector" #include "string" using namespace std; void __inline vSplitString( string strSrc ,vector<string>& vecDest ,char cSeparator ) { if( strSrc.empty() ) return ; string::size_type size_pos = 0; string::size_type size_prev_pos = 0; while( (size_pos = strSrc.find_first_of( cSeparator ,size_pos)) != string::npos) { string strTemp= strSrc.substr(size_prev_pos , size_pos-size_prev_pos ); vecDest.push_back(strTemp); //cout << "string = "<< strTemp << endl; size_prev_pos =++ size_pos; } vecDest.push_back(&strSrc[size_prev_pos]); //cout << "end string = "<< &strSrc[size_prev_pos] << endl; }; void main() { string strVal; //strVal="C:\\abc\\ddd"; //strVal="C:\\abc\\ddd中问\\csdn\\文件夹"; strVal="Z:\/?*\ddd"; vector<string> SplitVec; vSplitString( strVal, SplitVec, '\\') ; if (SplitVec.size()>0) { if(SplitVec[0].size()!=2) goto loop; const char *pan = SplitVec[0].c_str(); { if ( (*pan>='a'&&*pan<='z')||(*pan>='A'&&*pan<='Z')) { cout<<"盘符:"<<*pan<<",正常"<<endl; } else { goto loop; } } int npos; for (int i=1;i<SplitVec.size();i++) { npos = SplitVec[i].find('\\'); if (npos>0) { goto loop; } npos = SplitVec[i].find('/'); if (npos>0) { goto loop; } npos = SplitVec[i].find(':'); if (npos>0) { goto loop; } npos = SplitVec[i].find('*'); if (npos>0) { goto loop; } npos = SplitVec[i].find('?'); if (npos>0) { goto loop; } npos = SplitVec[i].find('"'); if (npos>0) { goto loop; } npos = SplitVec[i].find('<'); if (npos>0) { goto loop; } npos = SplitVec[i].find('>'); if (npos>0) { goto loop; } npos = SplitVec[i].find('|'); if (npos>0) { goto loop; } } cout<<strVal<<" 是正确的路径"<<endl; } else { goto loop; } return; loop: cout<<strVal<<" 是错误的路径"<<endl; }

首页
评论
分享
Top