关于数组取值的错误
我定义了一个全局数组,在读取txt文件时赋值,然后我想取值处理的时候发现赋值没有了,都是0。
这个是怎么回事?
而且是第一个值取出来是对的,后面的全都变成了初始化的0。求帮助。
Cpp里定义了
typedef struct node
{
double x;
double y;
double sq;
}node;
node *bigdata;
在菜单栏响应函数中
int BHang=3828;
bigdata=new node[BHang];
for(int i=0;i<BHang;i++)
{
bigdata[i].sq=0;
bigdata[i].x=0;
bigdata[i].y=0;
}
CString txtFileName;
CFileDialog bigdlg(TRUE,"系数",".txt",OFN_HIDEREADONLY
| OFN_OVERWRITEPROMPT,"txt(*.txt)|*.txt||");
if(bigdlg.DoModal()==IDOK)
{
txtFileName=bigdlg.GetPathName();
ifstream fouttxt;
fouttxt.open(txtFileName);
while(!fouttxt)
{
cout<<"数据打开失败!"<<endl;
return ;
}
char x;
CString bss;
int bcount=0;
double bvalue=0.0;
int brownumber=0;
while(!fouttxt.eof())
{
fouttxt.read(&x,1);
if (x==' ' || (int)x==9 || (int)x==10)
{
if (bss!="")
{
bvalue=atof(bss);//转换成double
bcount=bcount+1;//读入double数值的相母
int ny=0;
if(bcount%3==0)
{
bigdata[ny].sq=bvalue;
brownumber++;
ny=brownumber;
}
if(bcount%3==2)
{
bigdata[ny].y=bvalue;
}
if(bcount%3==1)
{
bigdata[ny].x=bvalue;
}
}
bss="";
}
else
{
if (x!='"' && x!=' ')
bss=bss+x;
}
}
fouttxt.close();
AfxMessageBox("数据正在处理,请等待……");
CString TxtFileName;
CFileDialog fileBDlg(FALSE);
fileBDlg.m_ofn.lpstrTitle = "另存为txt格式";
fileBDlg.m_ofn.lpstrFilter = "txt Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
fileBDlg.m_ofn.lpstrDefExt = "txt";
if(IDOK == fileBDlg.DoModal())
{
ofstream outbfile;
TxtFileName = fileBDlg.GetFileName();
outbfile.open(TxtFileName);
for(int ii=0;ii<BHang;ii++)
{
double oldvalue=0.0;
double newvalue=0.0;
oldvalue = bigdata[ii].sq+0.5;
newvalue = ceil(oldvalue);
outbfile<<bigdata[ii].x<<"\x20"<<bigdata[ii].y<<"\x20"<<newvalue<<"\n";
}
outbfile.close();
AfxMessageBox("存储完毕!");
}
}
delete []bigdata;
请问是怎么回事啊。谢谢了。