CString 中的ReverseFind只能反向查找单个字符,比如string.ReverseFind( 'd' ),但是我想反向查找"abc"就不能用ReverseFind,那该怎么办呢

citywolf 2005-03-21 02:10:58
CString 中的ReverseFind只能反向查找单个字符,比如string.ReverseFind( 'd' ),但是我想反向查找"abc"就不能用ReverseFind,那该怎么办呢?

比如我想反向查找CString str = "abcdeabe"中的ab,ReverseFind只是对单个字符查找,请问怎么样查找反向查找str中的ab呢?
...全文
1997 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanglei5695312 2010-06-21
  • 打赏
  • 举报
回复
str.ReverseFind
dragonyjd 2010-06-21
  • 打赏
  • 举报
回复
我的不可以,难道ATL下的CSTRING受此限制?
“ATL::CStringT<BaseType,StringTraits>::ReverseFind”: 不能将参数 1 从“const char [3]”转换为“char”
handwolf 2005-03-21
  • 打赏
  • 举报
回复
CString str = "abcdeabe";
int iPos = str.ReverseFind( "ad" );
//if the value of iPos is -1, it means there is no "ad" in the str.
handwolf 2005-03-21
  • 打赏
  • 举报
回复
CString str = "abcdeabe";
str.ReverseFind( "ad" );
yixiao0002000 2005-03-21
  • 打赏
  • 举报
回复
ReverseFind()可以查找字符串的
chenjunge 2005-03-21
  • 打赏
  • 举报
回复
CString 中的ReverseFind只能反向查找单个字符 谁说的
// ============================================================================= // FILE: StdString.h // AUTHOR: Joe O'Leary (with outside help noted in comments) // // If you find any bugs in this code, please let me know: // // jmoleary@earthlink.net // http://www.joeo.net/stdstring.htm (a bit outdated) // // The latest version of this code should always be available at the // following link: // // http://www.joeo.net/code/StdString.zip (Dec 6, 2003) // // // REMARKS: // This header file declares the CStdStr template. This template derives // the Standard C++ Library basic_string<> template and add to it the // the following conveniences: // - The full MFC CString set of functions (including implicit cast) // - writing to/reading from COM IStream interfaces // - Functional objects for use in STL algorithms // // From this template, we intstantiate two classes: CStdStringA and // CStdStringW. The name "CStdString" is just a #define of one of these, // based upone the UNICODE macro setting // // This header also declares our own version of the MFC/ATL UNICODE-MBCS // conversion macros. Our version looks exactly like the Microsoft's to // facilitate portability. // // NOTE: // If you you use this in an MFC or ATL build, you should include either // afx.h or atlbase.h first, as appropriate. // // PEOPLE WHO HAVE CONTRIBUTED TO THIS CLASS: // // Several people have helped me iron out problems and othewise improve // this class. OK, this is a long list but in my own defense, this code // has undergone two major rewrites. Many of the improvements became // necessary after I rewrote the code as a template. Others helped me // improve the CString facade. // // Anyway, these people are (in chronological order): // // - Pete the Plumber (???) // - Julian Selman // - Chris (of Melbsys) // - Dave Plummer // - John C Sipos // - Chris Sells // - Nigel Nunn // - Fan Xia // - Matthew Williams // - Carl Engman // - Mark Zeren // - Craig Watson // - Rich Zuris // - Karim Ratib // - Chris Conti // - Baptiste Lepilleur // - Greg Pickles // - Jim Cline // - Jeff Kohn // - Todd Heckel // - Ullrich Poll?hne // - Joe Vitaterna // - Joe Woodbury // - Aaron (no last name) // - Joldakowski (???) // - Scott Hathaway // - Eric Nitzche // - Pablo Presedo // - Farrokh Nejadlotfi // - Jason Mills // - Igor Kholodov // - Mike Crusader // - John James // - Wang Haifeng // - Tim Dowty // - Arnt Witteveen // - Glen Maynard // - Paul DeMarco // - Bagira (full name?) // - Ronny Schulz // - Jakko Van Hunen // - Charles Godwin // - Henk Demper // - Greg Marr // - Bill Carducci // - Brian Groose // - MKingman // - Don Beusee // // REVISION HISTORY // // 2005-JAN-10 - Thanks to Don Beusee for pointing out the danger in mapping // length-checked formatting functions to non-length-checked // CRT equivalents. Also thanks to him for motivating me to // optimize my implementation of Replace() // // 2004-APR-22 - A big, big thank you to "MKingman" (whoever you are) for // finally spotting a silly little error in StdCodeCvt that // has been causing me (and users of CStdString) problems for // years in some relatively rare conversions. I had reversed // two length arguments. // // 2003-NOV-24 - Thanks to a bunch of people for helping me clean up many // compiler warnings (and yes, even a couple of actual compiler // errors). These include Henk Demper for figuring out how // to make the Intellisense work on with CStdString on VC6, // something I was never able to do. Greg Marr pointed out // a compiler warning about an unreferenced symbol and a // problem with my version of Load in MFC builds. Bill // Carducci took a lot of time with me to help me figure out // why some implementations of the Standard C++ Library were // returning error codes for apparently successful conversions // between ASCII and UNICODE. Finally thanks to Brian Groose // for helping me fix compiler signed unsigned warnings in // several functions. // // 2003-JUL-10 - Thanks to Charles Godwin for making me realize my 'FmtArg' // fixes had inadvertently broken the DLL-export code (which is // normally commented out. I had to move it up higher. Also // this helped me catch a bug in ssicoll that would prevent // compilation, otherwise. // // 2003-MAR-14 - Thanks to Jakko Van Hunen for pointing out a copy-and-paste // bug in one of the overloads of FmtArg. // // 2003-MAR-10 - Thanks to Ronny Schulz for (twice!) sending me some changes // to help CStdString build on SGI and for pointing out an // error in placement of my preprocessor macros for ssfmtmsg. // // 2002-NOV-26 - Thanks to Bagira for pointing out that my implementation of // SpanExcluding was not properly handling the case in which // the string did NOT contain any of the given characters // // 2002-OCT-21 - Many thanks to Paul DeMarco who was invaluable in helping me // get this code working with Borland's free compiler as well // as the Dev-C++ compiler (available free at SourceForge). // // 2002-SEP-13 - Thanks to Glen Maynard who helped me get rid of some loud // but harmless warnings that were showing up on g++. Glen // also pointed out that some pre-declarations of FmtArg<> // specializations were unnecessary (and no good on G++) // // 2002-JUN-26 - Thanks to Arnt Witteveen for pointing out that I was using // static_cast<> in a place in which I should have been using // reinterpret_cast<> (the ctor for unsigned char strings). // That's what happens when I don't unit-test properly! // Arnt also noticed that CString was silently correcting the // 'nCount' argument to Left() and Right() where CStdString was // not (and crashing if it was bad). That is also now fixed! // // 2002-FEB-25 - Thanks to Tim Dowty for pointing out (and giving me the fix // for) a conversion problem with non-ASCII MBCS characters. // CStdString is now used in my favorite commercial MP3 player! // // 2001-DEC-06 - Thanks to Wang Haifeng for spotting a problem in one of the // assignment operators (for _bstr_t) that would cause compiler // errors when refcounting protection was turned off. // // 2001-NOV-27 - Remove calls to operator!= which involve reverse_iterators // due to a conflict with the rel_ops operator!=. Thanks to // John James for pointing this out. // // 2001-OCT-29 - Added a minor range checking fix for the Mid function to // make it as forgiving as CString's version is. Thanks to // Igor Kholodov for noticing this. // - Added a specialization of std::swap for CStdString. Thanks // to Mike Crusader for suggesting this! It's commented out // because you're not supposed to inject your own code into the // 'std' namespace. But if you don't care about that, it's // there if you want it // - Thanks to Jason Mills for catching a case where CString was // more forgiving in the Delete() function than I was. // // 2001-JUN-06 - I was violating the Standard name lookup rules stated // in [14.6.2(3)]. None of the compilers I've tried so // far apparently caught this but HP-UX aCC 3.30 did. The // fix was to add 'this->' prefixes in many places. // Thanks to Farrokh Nejadlotfi for this! // // 2001-APR-27 - StreamLoad was calculating the number of BYTES in one // case, not characters. Thanks to Pablo Presedo for this. // // 2001-FEB-23 - Replace() had a bug which caused infinite loops if the // source string was empty. Fixed thanks to Eric Nitzsche. // // 2001-FEB-23 - Scott Hathaway was a huge help in providing me with the // ability to build CStdString on Sun Unix systems. He // sent me detailed build reports about what works and what // does not. If CStdString compiles on your Unix box, you // can thank Scott for it. // // 2000-DEC-29 - Joldakowski noticed one overload of Insert failed to do a // range check as CString's does. Now fixed -- thanks! // // 2000-NOV-07 - Aaron pointed out that I was calling static member // functions of char_traits via a temporary. This was not // technically wrong, but it was unnecessary and caused // problems for poor old buggy VC5. Thanks Aaron! // // 2000-JUL-11 - Joe Woodbury noted that the CString::Find docs don't match // what the CString::Find code really ends up doing. I was // trying to match the docs. Now I match the CString code // - Joe also caught me truncating strings for GetBuffer() calls // when the supplied length was less than the current length. // // 2000-MAY-25 - Better support for STLPORT's Standard library distribution // - Got rid of the NSP macro - it interfered with Koenig lookup // - Thanks to Joe Woodbury for catching a TrimLeft() bug that // I introduced in January. Empty strings were not getting // trimmed // // 2000-APR-17 - Thanks to Joe Vitaterna for pointing out that ReverseFind // is supposed to be a const function. // // 2000-MAR-07 - Thanks to Ullrich Poll?hne for catching a range bug in one // of the overloads of assign. // // 2000-FEB-01 - You can now use CStdString on the Mac with CodeWarrior! // Thanks to Todd Heckel for helping out with this. // // 2000-JAN-23 - Thanks to Jim Cline for pointing out how I could make the // Trim() function more efficient. // - Thanks to Jeff Kohn for prompting me to find and fix a typo // in one of the addition operators that takes _bstr_t. // - Got rid of the .CPP file - you only need StdString.h now! // // 1999-DEC-22 - Thanks to Greg Pickles for helping me identify a problem // with my implementation of CStdString::FormatV in which // resulting string might not be properly NULL terminated. // // 1999-DEC-06 - Chris Conti pointed yet another basic_string<> assignment // bug that MS has not fixed. CStdString did nothing to fix // it either but it does now! The bug was: create a string // longer than 31 characters, get a pointer to it (via c_str()) // and then assign that pointer to the original string object. // The resulting string would be empty. Not with CStdString! // // 1999-OCT-06 - BufferSet was erasing the string even when it was merely // supposed to shrink it. Fixed. Thanks to Chris Conti. // - Some of the Q172398 fixes were not checking for assignment- // to-self. Fixed. Thanks to Baptiste Lepilleur. // // 1999-AUG-20 - Improved Load() function to be more efficient by using // SizeOfResource(). Thanks to Rich Zuris for this. // - Corrected resource ID constructor, again thanks to Rich. // - Fixed a bug that occurred with UNICODE characters above // the first 255 ANSI ones. Thanks to Craig Watson. // - Added missing overloads of TrimLeft() and TrimRight(). // Thanks to Karim Ratib for pointing them out // // 1999-JUL-21 - Made all calls to GetBuf() with no args check length first. // // 1999-JUL-10 - Improved MFC/ATL independence of conversion macros // - Added SS_NO_REFCOUNT macro to allow you to disable any // reference-counting your basic_string<> impl. may do. // - Improved ReleaseBuffer() to be as forgiving as CString. // Thanks for Fan Xia for helping me find this and to // Matthew Williams for pointing it out directly. // // 1999-JUL-06 - Thanks to Nigel Nunn for catching a very sneaky bug in // ToLower/ToUpper. They should call GetBuf() instead of // data() in order to ensure the changed string buffer is not // reference-counted (in those implementations that refcount). // // 1999-JUL-01 - Added a true CString facade. Now you can use CStdString as // a drop-in replacement for CString. If you find this useful, // you can thank Chris Sells for finally convincing me to give // in and implement it. // - Changed operators << and >> (for MFC CArchive) to serialize // EXACTLY as CString's do. So now you can send a CString out // to a CArchive and later read it in as a CStdString. I have // no idea why you would want to do this but you can. // // 1999-JUN-21 - Changed the CStdString class into the CStdStr template. // - Fixed FormatV() to correctly decrement the loop counter. // This was harmless bug but a bug nevertheless. Thanks to // Chris (of Melbsys) for pointing it out // - Changed Format() to try a normal stack-based array before // using to _alloca(). // - Updated the text conversion macros to properly use code // pages and to fit in better in MFC/ATL builds. In other // words, I copied Microsoft's conversion stuff again. // - Added equivalents of CString::GetBuffer, GetBufferSetLength // - new sscpy() replacement of CStdString::CopyString() // - a Trim() function that combines TrimRight() and TrimLeft(). // // 1999-MAR-13 - Corrected the "NotSpace" functional object to use _istpace() // instead of _isspace() Thanks to Dave Plummer for this. // // 1999-FEB-26 - Removed errant line (left over from testing) that #defined // _MFC_VER. Thanks to John C Sipos for noticing this. // // 1999-FEB-03 - Fixed a bug in a rarely-used overload of operator+() that // caused infinite recursion and stack overflow // - Added member functions to simplify the process of // persisting CStdStrings to/from DCOM IStream interfaces // - Added functional objects (e.g. StdStringLessNoCase) that // allow CStdStrings to be used as keys STL map objects with // case-insensitive comparison // - Added array indexing operators (i.e. operator[]). I // originally assumed that these were unnecessary and would be // inherited from basic_string. However, without them, Visual // C++ complains about ambiguous overloads when you try to use // them. Thanks to Julian Selman to pointing this out. // // 1998-FEB-?? - Added overloads of assign() function to completely account // for Q172398 bug. Thanks to "Pete the Plumber" for this // // 1998-FEB-?? - Initial submission // // COPYRIGHT: // 2002 Joseph M. O'Leary. This code is 100% free. Use it anywhere you // want. Rewrite it, restructure it, whatever. If you can write software // that makes money off of it, good for you. I kinda like capitalism. // Please don't blame me if it causes your $30 billion dollar satellite // explode in orbit. If you redistribute it in any form, I'd appreciate it // if you would leave this notice here. // ============
CString CUtilsDlg::GetExtName( CString name ) { CString strext = _T(""); CString strTemp = name; int i = strTemp.ReverseFind( '.' ); if( i != -1 ) { strext = strTemp.Right( strTemp.GetLength() - i - 1 ); } return strext; } void CUtilsDlg::DoToChar( CString mfile ) { CString mExt = GetExtName( mfile ); mExt.MakeLower(); if( ( mExt.Compare( _T("cpp") ) == 0 ) || ( mExt.Compare( _T("h") ) == 0 ) || ( mExt.Compare( _T("txt") ) == 0 ) ) { m_result.AddString( mfile ); CString mstrfile = mfile; m_before.SetSel( 0, -1 ); m_after.SetSel( 0, -1 ); m_before.Clear(); m_after.Clear(); m_before.SetWindowText(_T("")); m_after.SetWindowText(_T("")); //m_result.ResetContent(); CStdioFile file; if( !file.Open( mstrfile, CFile::modeRead ) ) { return; } CString strall; CString strSave; CString str; int i = 0; while( file.ReadString( str ) ) { CString strhead; strhead.Format( _T("%d"),i ); int nlen = strhead.GetLength(); switch( nlen ) { case 1: strhead += _T("------"); break; case 2: strhead += _T("-----"); break; case 3: strhead += _T("----"); break; case 4: strhead += _T("---"); break; case 5: strhead += _T("--"); break; case 6: strhead += _T("-"); break; } CString strafln = strhead; strafln += str; m_before.ReplaceSel( strafln ); m_before.ReplaceSel( _T("\r\n") ); CString strTemp = str; int nRet = -1; CString strTChar = ToTChar( strTemp, &nRet ); strSave += strTChar; strSave += _T("\r\n"); CString strbfln = strhead; strbfln += strTChar; m_after.ReplaceSel( strbfln ); m_after.ReplaceSel( _T("\r\n") ); if( nRet == 0 ) { CString strResult; strResult.Format("%d",i); m_result.AddString( strResult ); } i ++; } file.Close(); CFile fsave; if( fsave.Open( mstrfile, CFile::modeWrite ) ) { fsave.Write( strSave, strSave.GetLength() ); fsave.Close(); } } }
struct FOLDERINFO { int fileLength; //记录文件长度 char fileName[128]; //记录文件名 char filePath[512] //记录文件路径 }; 这里需要说明一下,为什么文件名,文件路径不用CString定义,原因是这个结构体我们要发给对方的,但是由于CString长度是不丁长的,导致结构体长度不能确定,从而使程序出错. 下面就是发送方的代码,我已经省去了目录选择和遍历目录的代码: void CMyQQDlg::SendFiles() { CSocket sockClient; sockClient.Create(); //创建Socket CString ip; m_You_IP.GetWindowText(ip); //m_You_IP是IP控件的变量 sockClient.Connect(ip, 11024); // /////////////////////////////////////////////////////////////////// CString fileName; ///////////////////////这是发送方处理目录的代码,如果没有这段代码 fileName=m_fileName; //将只能发送根目录下的文件:) int a=fileName.Find(filepathone); if(a!=2) { fileName=fileName.Right(fileName.GetLength()-a-1); fileName="C:\\"+fileName; } //////////////////////////////////////////////////////////////////////////// CFile myFile; FOLDERINFO myFileInfo; if(!myFile.Open(m_fileName,CFile::modeRead|CFile::typeBinary))//m_fileName是遍历后得到的文件 return ; myFileInfo.fileLength=myFile.GetLength(); //得到文件大小 strcpy(myFileInfo.fileName,myFile.GetFileName()); //得到文件名称 strcpy(myFileInfo.filePath,m_fileName); sockClient.Send(&myFileInfo,sizeof(myFileInfo)); //发送文件信息 myFile.Seek(0,CFile::begin); char m_buf[SIZEFILE]={0}; //缓冲区 CString strError; int num=0; int temp=0; //开始发送 for(;;) { num=myFile.Read(m_buf, SIZEFILE); if(num==0) break; end=sockClient.Send(m_buf, num); temp+=end; } myFile.Close(); sockClient.Close(); return ; } 下面就是接收方的代码: int CMyQQDlg::SaveYouFiles(CSocket &recSo, SOCKADDR_IN &client) { CString fnames; CString filenamepath; static int filescount=0; CString strfileIp,strfileName,strfileLength; char filemes[512]={0};//存文件名字的 int fileLengths=0; FOLDERINFO myFileInfo; recSo.Receive(&myFileInfo,sizeof(FOLDERINFO)); fileLengths=myFileInfo.fileLength; strfileIp.Format(inet_ntoa(client.sin_addr)); strfileName.Format(myFileInfo.fileName); //得到文件名字 strfileLength.Format("%f",myFileInfo.fileLength/1024.0); filenamepath.Format(myFileInfo.filePath); //得到文件路径 int a=filenamepath.ReverseFind(_T('\\'));//这5行代码是用来得到一个例如:\AMD\Intel\ATI\ filenamepath=filenamepath.Left(a+1); //主要是用来建立目录 CString b=filenamepath; a=b.ReverseFind(_T(':')); b=b.Right(b.GetLength()-a-1); filenamepath="files"+b; //所有的文件都存在files文件夹下 CreateDirectory(filenamepath,NULL); //创建目录 strcpy(filemes,filenamepath+strfileName); char bufs[SIZEFILE]={0}; //缓冲SIZEFILE=1024 CFile fs(filemes,CFile::modeCreate|CFile::modeWrite); //存文件 int n=0; //接受的字节数 0表示结束 int temp=0; //开始接收 for(;;) { n=recSo.Receive(bufs,SIZEFILE); //接受 if(n==0) //0表示结束 break; //接受完毕 fs.Write(bufs,n); // fputs(bufs,fp); temp+=n; } fs.Close(); return 0; }
/** ===================================================== 功能: CFolderContent类搜索一个目录, 列出该目录下的所有目录名称,列出所有子目录下的文件名称等属性。 作者: jef 作者邮箱: dungeonsnd@126.com 发步时间: 20100311 版本: v1.1 版权: 请遵循GNU. 对外接口: int GetAllSub(CString csPath) csPath: 一个目录或者一个完整的文件名 使用举例: void CGetFolderContentView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CString cs,csSaveFileName,csFileSave,csT,csT1; cs ="C:\\Documents and Settings\\All Users\\Documents\\My Music"; char chModule[8192]; memset(chModule,0,8192); GetModuleFileName(NULL,chModule,8192); csT.Format("%s",chModule); csT =csT.Left( csT.ReverseFind(_T('\\')) ); csSaveFileName =csT+_T("\\FolderContent输出文件.txt"); CRect rtClient; GetClientRect(&rtClient); CClientDC dc(this); dc.SetTextColor(RGB(200,80,80)); dc.FillSolidRect(rtClient,RGB(240,240,240)); CFolderContent fc; fc.GetAllSub(cs); int i,len,k; csT ="-------"; csT =cs+"总大小为"; csT1.Format(" %.4fMB,清单文件已保存在 %s",fc.m_dTotalSize/double(1024*1024),csSaveFileName); csT +=csT1; csT +="-------"; dc.TextOut(5,0,csT); csFileSave +=csT+_T("\r\n"); csT ="-------"; csT +="所有子目录如下:"; csT +="-------"; dc.TextOut(5,25,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_Directory.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecDirectorySize[i]/double(1024*1024)); csT1.Format(" 创建时间:%d年%d月%d日 ", fc.m_vecFileLastCreationTime[i].wYear, fc.m_vecFileLastCreationTime[i].wMonth, fc.m_vecFileLastCreationTime[i].wDay); csT =csT+csT1; csT =fc.m_Directory.GetAt(i)+csT; dc.TextOut(5,(i+2)*20,csT); csFileSave +=csT+_T("\r\n"); } k =i; csT ="-------"; csT +="所有文件如下:"; csT +="-------"; dc.TextOut(5,(k+3)*20,csT); csFileSave +=csT+_T("\r\n"); len =fc.m_PathNameExt.GetSize(); for (i=0;i<len;i++) { csT.Format(" 大小:%.4fMB",fc.m_vecFileSize[i]/double(1024*1024)); csT1.Format(" 修改时间:%d年%d月%d日 ", fc.m_vecFileLastWriteTime[i].wYear, fc.m_vecFile

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧