指定符号分割字符串

论坛 期权论坛 脚本     
匿名技术用户   2021-1-6 07:38   32   0

#include <Afx.h>
#include <iostream.h>

//以下是读一个文本,将文本中每行以,好分割字符到一个CStringArray ,并读出行中分割数和每个CStringArray
int main(int argc, char* argv[])
{

CString lu;
char kk[35];
cout<<"请输入路径"<<endl;
cin>>kk;
cout<<endl;
lu=kk;
CStdioFile file;
if(file.Open(lu,CFile::shareDenyNone))
{
BOOL isEof=false;
CString str;
while(!isEof)
{
isEof=!file.ReadString(str);
if(isEof) break;

str.TrimLeft();
str.TrimRight();
if(str.Right(1)!=",")str+=",";
int pos=-1;
int k=0;
CString temp;
CStringArray array;

while((pos=str.Find(",",0))!=-1)
{
k++;
temp=str.Left(pos);

if(!temp.IsEmpty()&&temp!="")
{
array.Add(temp);

}else
{
array.Add(_T(" "));
}
str=str.Right(str.GetLength()-pos-1);
}
cout<<"+++++++++++++++++++"<<k<<endl;

for(int i=0;i<k;i++)
{
cout<<array.GetAt(i)<<endl;
}
}
}

return 0;
}

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:7942463
帖子:1588486
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP