Regex++ causes abnormal program termination
-
Here is my code CStdioFile myfile,newFile; CString inString = ""; char* fString=""; //char *pattern="((\\s*lmp\\s*))"; char *pattern="\\s*(LMP\\([0-9]+\\)).*)\\n"; //***********The line below when reached //causes abnormal termination,dont know /whats the problem Please help RegEx exp(pattern,TRUE); std::string wholeFileStr=""; CString wholeFileString = "",filname="c:\\test23.txt",nName="c:\\MunnaMunna.txt"; // Read entire file into a string. try{ myfile.Open(filname,CFile::modeRead | CFile::typeText, NULL); newFile.Open(nName,CFile::modeWrite , NULL); } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } try{ while (myfile.ReadString(inString)) {//newFile.WriteString(inString); //wholeFileString += inString; wholeFileStr = inString.GetBuffer(2); // //const char *wholeFileStr=(LPCTSTR)inString;//.GetBuffer(10); // RegEx exp("(\\*sLMP((0-9)+\)).*)\n$",TRUE); // RegEx exp("(\s*LMP\([0-9]+\))",TRUE); // RegEx exp("\s*LMP",TRUE); if(exp.Search(wholeFileStr)) { //strcpy(fString,exp[1].c_str()); CString sd(exp[1].c_str()); AfxMessageBox(sd); } else if(fString!="") { CString temp(fString); //CString temp2(wholeFileStr); temp+=inString; temp+="\n"; //strcat(fString,wholeFileStr); //strcat(fString,"\n"); newFile.WriteString(temp); } } //*************************************************************** } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } newFile.Close(); myfile.Close();
-
Here is my code CStdioFile myfile,newFile; CString inString = ""; char* fString=""; //char *pattern="((\\s*lmp\\s*))"; char *pattern="\\s*(LMP\\([0-9]+\\)).*)\\n"; //***********The line below when reached //causes abnormal termination,dont know /whats the problem Please help RegEx exp(pattern,TRUE); std::string wholeFileStr=""; CString wholeFileString = "",filname="c:\\test23.txt",nName="c:\\MunnaMunna.txt"; // Read entire file into a string. try{ myfile.Open(filname,CFile::modeRead | CFile::typeText, NULL); newFile.Open(nName,CFile::modeWrite , NULL); } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } try{ while (myfile.ReadString(inString)) {//newFile.WriteString(inString); //wholeFileString += inString; wholeFileStr = inString.GetBuffer(2); // //const char *wholeFileStr=(LPCTSTR)inString;//.GetBuffer(10); // RegEx exp("(\\*sLMP((0-9)+\)).*)\n$",TRUE); // RegEx exp("(\s*LMP\([0-9]+\))",TRUE); // RegEx exp("\s*LMP",TRUE); if(exp.Search(wholeFileStr)) { //strcpy(fString,exp[1].c_str()); CString sd(exp[1].c_str()); AfxMessageBox(sd); } else if(fString!="") { CString temp(fString); //CString temp2(wholeFileStr); temp+=inString; temp+="\n"; //strcat(fString,wholeFileStr); //strcat(fString,"\n"); newFile.WriteString(temp); } } //*************************************************************** } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } newFile.Close(); myfile.Close();
My guess would be that RegExp is throwing an exception because the regular expression passed to it in the constructor is invalid (i'm not a regexp guru though, so am not able to verify that your regexp is correct). I suggest you check your expression, and also check the docs for RegExp - it may throw an exception that tells you exactly what is wrong. At the moment you are ignoring any such exceptions, and hence the program terminates. Dave
-
Here is my code CStdioFile myfile,newFile; CString inString = ""; char* fString=""; //char *pattern="((\\s*lmp\\s*))"; char *pattern="\\s*(LMP\\([0-9]+\\)).*)\\n"; //***********The line below when reached //causes abnormal termination,dont know /whats the problem Please help RegEx exp(pattern,TRUE); std::string wholeFileStr=""; CString wholeFileString = "",filname="c:\\test23.txt",nName="c:\\MunnaMunna.txt"; // Read entire file into a string. try{ myfile.Open(filname,CFile::modeRead | CFile::typeText, NULL); newFile.Open(nName,CFile::modeWrite , NULL); } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } try{ while (myfile.ReadString(inString)) {//newFile.WriteString(inString); //wholeFileString += inString; wholeFileStr = inString.GetBuffer(2); // //const char *wholeFileStr=(LPCTSTR)inString;//.GetBuffer(10); // RegEx exp("(\\*sLMP((0-9)+\)).*)\n$",TRUE); // RegEx exp("(\s*LMP\([0-9]+\))",TRUE); // RegEx exp("\s*LMP",TRUE); if(exp.Search(wholeFileStr)) { //strcpy(fString,exp[1].c_str()); CString sd(exp[1].c_str()); AfxMessageBox(sd); } else if(fString!="") { CString temp(fString); //CString temp2(wholeFileStr); temp+=inString; temp+="\n"; //strcat(fString,wholeFileStr); //strcat(fString,"\n"); newFile.WriteString(temp); } } //*************************************************************** } catch (CFileException e) { MessageBox("The file " + filname + " could not be opened for reading", "File Open Failed", MB_ICONHAND|MB_ICONSTOP|MB_ICONERROR ); //return FALSE; //myfile.Close(); //newFile.Close(); } newFile.Close(); myfile.Close();
DOCs say the 2nd parameter of the constructor of RegEx is a RegexOptions (A bitwise OR combination of RegexOption enumeration values) and not a BOOL.
-
DOCs say the 2nd parameter of the constructor of RegEx is a RegexOptions (A bitwise OR combination of RegexOption enumeration values) and not a BOOL.
I am using the CRegex class as am not good in using stl,i know now the pattern to be used for searching is not correct but dont know what the problem is ,basically what i am trying to do is look for lmp() in each line and remove new lines after all the lines that dont have a line wih lmp() beofre them,that way all the lines will start with lmp()