everything seems to go fine except when i get to the search. either the program runs with a runtime error ,and it cannot be excuted.

/*****************************************************
 Program Filename	 : post.cpp
 Date			     : May 27, 2007
 Purpose			 : NC code convert		
 Input from			 : a cls data file
 Output to			 : a NC data file

 *****************************************************/

// Preprocessor directives:

#include <iostream>
#include <string>
#include <cstring>
#include<cmath>
#include<fstream>
#include<cstdlib>
#include<conio.h>
#include<process.h>

using namespace std;

const float PI =float (3.14159);			// define PI value

void main()
{
	// Variable declarations:
	
	char infile[20],outfile[20];							 //定义外部文件名
	fstream outdata,indata;									//定义内部文件名

	// Function prototypes:

	void NCcode_convert(fstream &,fstream &);

	// Function body:

	cout<<"\nPlease enter the external cut file to Post: ";    //提示输入CLS文件路径
	cin.getline(infile,20);									   //以字符变量形式存放外部文件名	

	indata.open(infile,ios::in);								//打开文件
    if(indata.fail())								        	//检查文件打开成功与否
	{
		cerr<<"\n***>Open error while opening input file "<<infile<<endl;		
		exit(-1);
	}

	cout<<"\nPlease enter the name of Post file: ";
	cin.getline (outfile,20);
	
	outdata.open(outfile,ios::out);
	if(outdata.fail())
	{
		cerr<<"\n***> Open error while opening input file "<<outfile;
		exit(-1);
	}
	
	cout<<"\nPost from"<<infile<<" to "<<outfile<<" in the process...\n";

	NCcode_convert(indata,outdata);	

	cout<<"\nPost process completed..."<<endl;

	indata.close();											//文件关闭
	outdata.close();
}

//......................................................................
/******************************************************
Function Name   :	NCcode_convert
Purpose         :   convert cls code to NCcode
Called by       :	main  
******************************************************/

void NCcode_convert(/* I-O */ fstream &indata,fstream &outdata)
{
	// Local variable declarations:

	char *ptr;												//定义指针变量ptr
	char *temp1= "GOTO", *temp2 = "FEDRAT", *temp3 = "CIRCLE";	// 刀位文件命令	
	char *temp4="LOAD/TOOL",*temp5="RAPID",*temp6="SPINDL";
	char ch;
    char axe[10];
	char str[128];								     	
    int n,m,sz,f,tempf=0;
    int nnn,nn[8],t,t01;	
    int flag=1;
    int N=1,n02=0;
	float x,y,z,i,j,k,a,c,fx,fy,fz,fa,fc;
	float ForthAxisMin,ForthAxisMax,FifthAxisMin,FifthAxisMax;
    float tempa=-999.0;
	
	// Function body:

	while(!indata.eof())					//通过循环从文件中读取数据
	{		
           indata>>str;							//从文件中读取一条记录并将内容存放到变量str
		   m=0;
		   ptr=strstr(str,temp1);			    //读取 FEDRAT   m=1		   
		   if(ptr!=NULL)
			   m=1;
		   ptr=strstr(str,temp2);				//读取 GOTO     m=2
		   if(ptr!=NULL)
			   m=2;		   
		   ptr=strstr(str,temp3);				//读取  CIRCLE  m=3
		   if(ptr!=NULL)
			   m=3;
		   ptr=strstr(str,temp4);				//读取  SPINDL  m=4
		   if(ptr!=NULL)
			   m=4;

		   switch(m)
		   {
		   case 1:                                //读出FEDRAT中的值
			   {
				sz=strlen(str);
				t=1;
				for(nnn=0;nnn<=sz;nnn++)
				{
					ch=str[nnn];
				    if((ch=='/')||(ch==','))
					{
						nn[t]=nnn;
						t++;
					}
				}
				n02+=1;	
				for(nnn=1;nnn<t;nnn++)
				{
					int cn1,op=0;			    	//数组和计数器
					if(nnn==2&&n02==1) 
					{
						for(cn1=(nn[nnn]+1);cn1<(sz-1);cn1++)
						{ 
							axe[op]=str[cn1];
							op++;
						}
					           f=atof(axe);
					}
					if(nnn==1&&n02>=2)
					{
						for(cn1=(nn[nnn]+1);cn1<(sz-1);cn1++)    //FEDRAT中的值第二次以上出现时
						{ 
							axe[op]=str[cn1];
							op++;
						}
						f=atof(axe);
					}
				}
				flag=2;
				break;
			   }
			   case 2:					//读出GOTO中的X,Y,Z,I,J,K值
			   {
				   sz=strlen(str);
				   t=1;
				   for(nnn=0;nnn<=sz;nnn++)
				   {
					   ch=str[nnn];
					   if((ch=='/')||(ch==','))
					   {
						   nn[t]=nnn;
						   t++;
					   }
				   }
				   for(nnn=1;nnn<t;nnn++)
				   {
					   int cn1=0,op=0;
					   if(nnn<=6) 
						   switch(nnn)
					   {
		   case 1:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(nn[nnn+1]-1);cn1++)      //X data
				   {
					   axe[op]=str[cn1];
					   op++;
				   }
				   x=atof(axe);
				   break;
			   }
		   case 2:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(nn[nnn+1]-1);cn1++)             //Y data
				   {
					   axe[op]=str[cn1];
					   op++;
				   }
				   y=atof(axe);
				   break;
			   }
		   case 3:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(nn[nnn+1]-1);cn1++)          //Z data
				   {
					   axe[op]=str[cn1];
					   op++;
				   }
				   z=atof(axe);
				   break;
			   }
		   case 4:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(nn[nnn+1]-1);cn1++)        //I data
				   { 
					   axe[op]=str[cn1];
					   op++;
				   }
				   i=atof(axe);
				   break;
			   }
		   case 5:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(nn[nnn+1]-1);cn1++)         //J data
				   { 
					   axe[op]=str[cn1];
					   op++;
				   }
				   j=atof(axe);
				   break;
			   }
		   case 6:
			   {
				   for(cn1=(nn[nnn]+1);cn1<(sz-1);cn1++)          //K data
				   { 
					   axe[op]=str[cn1];
					   op++;
				   }
				   k=atof(axe);
				   break;
			   }
					   }    //end switch(nnn)
				   }		//end for

				   ForthAxisMin=0;
				   ForthAxisMax=120;		//绕X转角
				   FifthAxisMin=0;
				   FifthAxisMax=360;		//绕Z转角
	       // A、C转角计算
				   if (z>0)
					   fa  = atan( sqrt(x*x+y*y)/z);
				   if (z=0)
					   fa = 90;
				   if (z<0 || fa<120)
					   fa = 90 - atan(sqrt(x*x+y*y)/z);

				   if  ((x>0 && y>=0)||(x>0 && y<=0))
					   fc=90+atan(y/x);
				   if ((x<0 && y>=0)||(x<0 && y<=0))
					   fc = 270+atan(y/x);
				   if (x=0 && y<=0)
					   fc = 0;
				   if (x=0 && y>0)
					   fc = 180;

			// 机床运动坐标X、Y、Z计算
				   fx = x*cos(fc) + y*sin(fc);
				   fy = -x*sin(fc)*cos(fa) + y*cos(fc)*cos(fa) + z*sin(fa);
				   fz = x*sin(c)*sin(fa) - y*cos(fc)*sin(fa) + z*cos(fa);
		
		if(flag==2)
		{	 
			  outdata<<fixed;
			  outdata.precision (3);
			  outdata<<"\n"<<"G01"<<" X"<<fx<<" Y"<<fy<<" Z"<<fz<<" A"<<fa<<" C"<<fc<<" F"<<f;
		}

		else
		{
			 outdata<<fixed;
			 outdata.precision (3);
		     outdata<<"\n"<<" X"<<fx<<" Y"<<fy<<" Z"<<fz<<" A"<<fa<<" C"<<fc;
		}

		N=N+1; 
		flag=1;
		break;
			}	//end case 2
		   }	//end switch
	}			//end while
}

and need your help, thank u.

here is the inputfile

============================================================
TOOL PATH/VARIABLE_CONTOUR,TOOL,R20
TLDATA/MILL,20.0000,10.0000,75.0000,0.0000,0.0000
MSYS/941.3497,-163.0247,275.8736,-0.5905862,-0.7855187,0.1848464,-0.8025755,0.5478651,-0.2360431
$$ centerline data
PAINT/PATH
PAINT/SPEED,10
LOAD/TOOL,2
PAINT/COLOR,186
RAPID
GOTO/459.8443,1190.0016,210.4375,0.3473078,0.0002385,0.9377512
PAINT/COLOR,6
FEDRAT/MMPM,250.0000
GOTO/457.5000,1190.0000,204.1076
PAINT/COLOR,31
GOTO/438.4375,1190.0000,212.4764,0.3804132,0.0021705,0.9248141
PAINT/COLOR,1
RAPID
GOTO/441.0053,1190.0147,218.7189
PAINT/COLOR,186
RAPID
GOTO/300.5092,1190.0079,230.8892,0.0407101,0.0011702,0.9991703
PAINT/COLOR,6
GOTO/300.2344,1190.0000,224.1448
PAINT/COLOR,31
GOTO/295.4688,1190.0000,224.3390,0.0407617,0.0039371,0.9991611
GOTO/285.9375,1190.0000,224.7301,0.0412627,0.0092274,0.9991057
GOTO/266.8750,1190.0000,225.4996,0.0376068,0.0124482,0.9992151
GOTO/257.3438,1190.0000,225.8560,0.0380371,0.0049857,0.9992639
GOTO/252.5781,1190.0000,226.0403,0.0391564,0.0008523,0.9992327
PAINT/COLOR,1
RAPID
GOTO/252.8424,1190.0058,232.7851
PAINT/COLOR,186
RAPID
GOTO/195.8175,1190.0523,235.5088,0.0632478,0.0077504,0.9979678
PAINT/COLOR,6
GOTO/195.3906,1190.0000,228.7725
PAINT/COLOR,31

>the program runs with a runtime error
Make sure you're terminating all of your built strings with '\0'. It doesn't look like you're doing that with some of the arguments to atof, and that could very well give you a runtime error.

Of course, you have issues across the board, but they don't pertain to your question and I don't feel like pointing them all out.

>the program runs with a runtime error
Make sure you're terminating all of your built strings with '\0'. It doesn't look like you're doing that with some of the arguments to atof, and that could very well give you a runtime error.

Of course, you have issues across the board, but they don't pertain to your question and I don't feel like pointing them all out.

I think I should check it once more ,and it still exits some problems, why?

I think if you point some in detail, it would be better. thanks~~~~~~~~

>I think if you point some in detail, it would be better. thanks~~~~~~~~
Yes, that's the problem. You're overrunning one of the strings because it doesn't have a null character at the end. In my test, it corrupted temp1 and the failure occurred on the first call to strstr.

>I In my test, it corrupted temp1 and the failure occurred on the first call to strstr.

YES, I also find it, and I try serval approches, but it didn't work.

If you had modified it, please send it to my email:[email removed].:)

>YES, I also find it, and I try serval approches, but it didn't work.
You have to fix the cause of the problem. That means properly terminating any strings that you build before using them. For example:

for(cn1=(nn[nnn]+1);cn1<(sz-1);cn1++)
{ 
  axe[op]=str[cn1];
  op++;
}
 f=atof(axe);

At the end of this loop, axe isn't terminated. You need to add '\0' to the end of it because that's what atof expects. Also, in my test this is where temp1 is corrupted. You totally overrun the bounds of axe (which is of size 10) by writing 50-something characters to it. axe should probably be quite a bit larger.

oh,oh,,
may be i need a more directly modified prog____

>may be i need a more directly modified prog
In other words, you don't understand what I'm saying, can't follow instructions, and want me to fix it for you. It compiles and runs if I change char axe[10]; to char axe[100]; . I have no idea if the output is correct, but that's your problem, not mine. Is that direct and simple enough for you?

^_^ I think I have understood your mean and your behaviour, another problem?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.