急,文件读写问题,请高手支招

aeoluszhou 2003-09-13 02:59:01
源程序(使用win32 console application新建的工程):
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void outputLine(int,double,double,double);

int main()
{
double a=10,b=20;
double min_distance=100000;
ifstream readFile("E:\\data\\aa.dat",ios::in | ios::out);

if(!readFile)
{
cerr<<"file could not be opened\n";
exit(1);
}

char command[100]="ABC",character[100];
int node=1,index;
double x,y,z;
char comp1,comp2,comp3;

cout<<setiosflags(ios::left)<<setw(20)<<"node"
<<setw(20)<<"x"<<setw(20)<<"y"
<<setw(20)<<"z"<<"\n";

do
readFile>>command;
while(strcmp(command,".NOE")!=0);

readFile>>character>>index>>comp1>>x>>comp2>>y>>comp3>>z;

while(strcmp(character,"I")==0)
{
double temp=pow((x-a),2)+pow((y-b),2);
if(temp<min_distance)
{
min_distance=temp;
node=index;
}
cout<<command<<endl;
outputLine(index,x,y,z);
readFile>>character>>index>>comp1>>x>>comp2>>y>>comp3>>z;
}

readFile>>command;/////////////////////////////////////////////

cout<<command<<endl
<<node<<endl;

readFile.clear();
readFile.seekg(0);
return 0;
}

void outputLine(int a,double b,double c,double d)
{
cout<<setiosflags(ios::left)<<setw(20)<<a
<<setw(20)<<setprecision(7)<<b<<
setw(20)<<setprecision(7)<<c<<
setw(20)<<setprecision(7)<<d<<endl;
}
被读取的文件格式如下(dat文件):
.DEL.*
!
! "/L_1" Set by BOSS
! "/L_1" Set by BOSS
! "/L_1" Set by BOSS
ABRE '/L_1' '335.'
! "/L_2" Set by BOSS
! "/L_2" Set by BOSS
! "/L_2" Set by BOSS
ABRE '/L_2' '4.49E2'
! "/L_3" Set by BOSS
! "/L_3" Set by BOSS
! "/L_3" Set by BOSS

.NOE
I 1 X 0.00000000E+00 Y 0.00000000E+00 Z 0.00000000E+00
I 2 X 1.52400000E+02 Y 0.00000000E+00 Z 0.00000000E+00
I 3 X 1.52400000E+02 Y 1.27000000E+02 Z 0.00000000E+00
I 4 X 0.00000000E+00 Y 1.27000000E+02 Z 0.00000000E+00
I 5 X 2.54000000E+01 Y 2.54000000E+01 Z 0.00000000E+00
I 6 X 2.54000000E+01 Y 1.01600000E+02 Z 0.00000000E+00
I 7 X 1.27000000E+02 Y 1.01600000E+02 Z 0.00000000E+00

.MAI
I 1 ATT 1 1 N $
38 39 77 74 0 146 $
147 185 182
I 2 ATT 1 1 N $
74 77 78 6 0 182 $

.CLM NOE /L_1 FIX COMP
NOE /L_2 FIX COMP
NOE /L_3 FIX COMP

现在问题处在readFile>>command;(用“///////”标出行),原因是,不能用变量从文件中读出新的字符串,command中存放的还是.NOE,而我期望的是从当前所读至的位置向下读取新的字符串。
不知其中的原因,请高人、大侠们不吝指教。谢谢。
...全文
30 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

69,336

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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