已经编写好的逐行读取文件的C++程序和数值计算程序,但是我不清楚怎么把逐行读取的数值保存到变量中保存到x y z i j k u v w中,然后进行计算。请各位老师指点。第一次发表博问,请给位老师见谅.
比如未处理的数据如下:(小写字母行的字母是我自行加上的,GOTO后的数据分别要赋值到x y z i j k u v w中,各数据间使用逗号分开的,$$后面的三个数据是u v w)
小写字母: x y z i j k u v w
GOTO/23.4932,-81.4323,107.7596,-0.6389673,-0.4383417,0.6321213 $$ 23.4397,-81.6183,107.5881
GOTO/23.4202,-81.2891,107.7604,-0.6391031,-0.4390556,0.6314882 $$ 23.3878,-81.4743,107.6062
GOTO/23.3478,-81.1456,107.7612,-0.6392378,-0.4397696,0.6308548 $$ 23.3212,-81.3314,107.6113
GOTO/23.2754,-81.0020,107.7620,-0.6393714,-0.4404837,0.6302209 $$ 23.2486,-81.1889,107.6112
GOTO/23.2030,-80.8585,107.7628,-0.6395038,-0.4411979,0.6295866 $$ 23.1760,-81.0464,107.6110
逐行读取文件的程序如下:
using namespace std;
int main(){
string s;
ifstream inf;
inf.open("F://九坐标生成.txt"); //打开F为文件所在位置,
//打开输出文件
ofstream outf;
outf.open("F://out.txt");
while (getline(inf, s)) //getline(inf,s)是逐行读取inf中的文件信息
{
outf << s << '\n';
cout << s << endl << endl;
}
inf.close();
outf.close();
return 0;
}
然后读取文件后把得到的x y z i j k u v w进行如下计算(程序已经编写好):
int main()
{
float i=0.0f,j=0.0f,k=0.0f; //定义坐标轴矢量;
float A=0.0f,C=0.0f; //定义旋转轴的角度;
float x=0.0f,y=0.0f,z=0.0f; //工件坐标系下的刀心点x y z值;
float X=0.0f,Y=0.0f,Z=0.0f; /机床坐标系下的刀心点X Y Z值/
float u=0.0f,v=0.0f,w=0.0f; /工件坐标系下的切触点u v w值/
float U=0.0f,V=0.0f,W=0.0f; /机床坐标系下的切触点U V W值/
//输入各工件坐标系下的坐标值
printf("请输入工件坐标系下x y z i j k u v w的值:");
scanf("%f %f %f %f %f %f %f %f %f",&x,&y,&z,&i,&j,&k,&u,&v,&w);
//坐标值的计算
A=acos(k);
if((k!=-1&&j==0&&i>0)||(k!=1&&j==0&&i>0))
printf("C=PI/2");
else if((k!=-1&&j==0&&i<0)||(k!=1&&j==0&&i<0))
printf("C=-PI/2");
else if(k==-1||k==1)
printf("C为任意值");
else
C=atan(i/-j);
X=x*cos(C)+y*sin(C);
Y=-x*cos(A)*sin(C)+y*cos(A)*cos(C)+(z+d)*sin(A);
Z=-d+x*sin(A)*sin(C)-y*sin(A)*cos(C)+(z+d)*cos(A)-m;
U=u*cos(C)+v*sin(C);
V=-u*cos(A)*sin(C)+v*cos(A)*cos(C)+(w+d)*sin(A);
W=-d+u*sin(A)*sin(C)-v*sin(A)*cos(C)+(w+d)*cos(A)-m;
printf("\nX Y Z A C U V W的值是X%.3f Y%.3f Z%.3f A%.3f C%.3f U%.3f V%.3f W%.3f",X,Y,Z,A*180/PI,C*180/PI,U,V,W);
return 0;
}
各位老师,请问如何将读取文件中的数据分别保存到x y z i j k u v w中进行运算。
你要的程序:
#include<fstream> //ifstream
#include<iostream>
#include<string> //包含getline()
#include<cmath>
using namespace std;
int main(){
string s;
ifstream inf;
inf.open("/Users/xiaokang/Desktop/test/test.txt"); //打开F为文件所在位置,
//打开输出文件
ofstream outf;
outf.open("test.txt");
int tag =0;
int pos =0;
while (getline(inf, s)) //getline(inf,s)是逐行读取inf中的文件信息
{
double value[1005];
tag=0;
string s1 = s.substr(5,s.length()-5);
string s2 = "";
for(int i=0;i<s1.length();i++)
{
if(s1[i]==','||s1[i]==' ')
{
value[tag++] = atof(s2.c_str());
s2="";
}
else if(s1[i]!='$')
{
s2+=s1[i];
}
}
if(s2!="")
value[tag++]=atof(s2.c_str());
for(int i=0;i<tag;i++)
{
cout<<value[i]<<" ";
}
cout<<endl;
}
inf.close();
outf.close();
return 0;
}
记得路径要是绝对路径。<br/>
我只在文件中放了第一行。<br/>
输出结果:
23.4932 -81.4323 107.76 -0.638967 -0.438342 0.632121 0 23.4397 -81.6183 107.588
value数组
是保存xyz....的值的数组
程序好像有点不正确,我是在VS2013-VC++上运行的。显示结果是下边这样
老师您看下
@ZHuaHua: 程序修改了。
文件里的内容是:
GOTO/23.4932,-81.4323,107.7596,-0.6389673,-0.4383417,0.6321213 $$ 23.4397,-81.6183,107.5881
GOTO/23.4202,-81.2891,107.7604,-0.6391031,-0.4390556,0.6314882 $$ 23.3878,-81.4743,107.6062
GOTO/23.3478,-81.1456,107.7612,-0.6392378,-0.4397696,0.6308548 $$ 23.3212,-81.3314,107.6113
GOTO/23.2754,-81.0020,107.7620,-0.6393714,-0.4404837,0.6302209 $$ 23.2486,-81.1889,107.6112
GOTO/23.2030,-80.8585,107.7628,-0.6395038,-0.4411979,0.6295866 $$ 23.1760,-81.0464,107.6110
输出:
23.4932 -81.4323 107.76 -0.638967 -0.438342 0.632121 0 23.4397 -81.6183 107.588
23.4202 -81.2891 107.76 -0.639103 -0.439056 0.631488 0 23.3878 -81.4743 107.606
23.3478 -81.1456 107.761 -0.639238 -0.43977 0.630855 0 23.3212 -81.3314 107.611
23.2754 -81.002 107.762 -0.639371 -0.440484 0.630221 0 23.2486 -81.1889 107.611
23.203 -80.8585 107.763 -0.639504 -0.441198 0.629587 0 23.176 -81.0464 107.611
请文件路径设置正确,其次文件里的数据格式如你写的那样,不能有差错。