指定符号分割字符串

来源:互联网 发布:淘宝自动下架时间 编辑:程序博客网 时间:2024/04/30 10:58

#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;
}
 

原创粉丝点击