使用文件指针出现问题,求大神帮助

py-python 2017-03-30 08:54:33

我调试的时候
这个函数 运行第一遍可以,第二遍就出错了
但是我明明定义了CR,用VC++6.0调试的时候第二遍告诉我说找不到CR
virtual void read(fstream &file)
{

file>>rx>>ry>>r>>wx>>wy;
char a[5];
file>>a;
int alen=strlen(a);
ccrwords=new char[alen+1];
char b[5];
file>>b;
int blen=strlen(b);
CGraph::color=new char[blen+1];
strcpy(CGraph::color,b);
file>>CGraph::linewidth;
}
全部的代码是
// 练习2改(3).cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<fstream>
using namespace std;
class CGraph
{
public:
char *color;
int linewidth;
char *type;
virtual void read(fstream &file){};
virtual void ShowMsg()=0;
virtual void save(fstream &file){};
void show()
{
cout<<"type"<<type<<endl;
cout<<"color"<<color<<endl;
cout<<"linewidth"<<linewidth<<endl;
}
friend class CPPage;
};

class CPPage;

class CRect:virtual public CGraph
{
private:
int lx;
int ly;
int wx;
int wy;
char *crwords;
public:
CRect()
{
lx=0;
ly=0;
wx=0;
wy=0;
crwords=NULL;
}
virtual void read(fstream &file)
{
file>>lx>>ly>>wx>>wy;
char a[5];
file>>a;
int alen=strlen(a);
crwords=new char[alen+1];
strcpy(crwords,a);
char b[5];
file>>b;
int blen=strlen(b);
CGraph::color=new char[blen+1];
strcpy(CGraph::color,b);
file>>CGraph::linewidth;
CGraph::type=new char[4];
strcpy(CGraph::type,"Rect");
}
virtual void ShowMsg()
{
show();
}
void show()
{
CGraph::show();
cout<<"digonal:"<<"("<<lx<<","<<ly<<")"<<"("<<wx<<","<<wy<<")"<<endl;
cout<<"message:"<<crwords<<endl;
}
virtual void save(fstream &file)
{
file<<lx<<" "<<ly<<" "<<wx<<" "<<wy<<" "<<crwords<<" "<<color;
}
~CRect()
{
if(crwords!=NULL)
{
delete[] crwords;
crwords=NULL;
}
}
friend class CPPage;
};

class CCircle:virtual public CGraph
{
private:
int rx;
int ry;
int r;
char *ccwords;
public:
CCircle()
{
rx=0;
ry=0;
r=0;
ccwords=NULL;
color=NULL;
linewidth=0;
}
virtual void read(fstream &file)
{
file>>rx>>ry>>r;
char a[5];
file>>a;
int alen=strlen(a);
ccwords=new char[alen+1];
strcpy(ccwords,a);
char b[5];
file>>b;
int blen=strlen(b);
CGraph::color=new char[blen+1];
strcpy(CGraph::color,b);
file>>CGraph::linewidth;
CGraph::type=new char[7];
strcpy(CGraph::type,"Circle");
}
virtual void ShowMsg()
{
show();
}
void show()
{
cout<<"center:"<<"("<<rx<<","<<ry<<")"<<"r"<<endl;
CGraph::show();
cout<<"message:"<<ccwords<<endl;
}
virtual void save(fstream &file)
{
file<<rx<<" "<<ry<<" "<<r<<" "<<ccwords<<" "<<color;
}
~CCircle()
{
if(ccwords!=NULL)
{
delete[] ccwords;
ccwords=NULL;
}
}
friend class CPPage;
};

class CCircleRect:public CRect,public CCircle
{
private:
int rx;
int ry;
int r;
int lx;
int ly;
int wx;
int wy;
char *ccrwords;
public:
CCircleRect()
{
rx=0;
ry=0;
r=0;
lx=0;
ly=0;
wx=0;
wy=0;
}
virtual void read(fstream &file)
{

file>>rx>>ry>>r>>wx>>wy;
char a[5];
file>>a;
int alen=strlen(a);
ccrwords=new char[alen+1];
char b[5];
file>>b;
int blen=strlen(b);
CGraph::color=new char[blen+1];
strcpy(CGraph::color,b);
file>>CGraph::linewidth;
}
virtual void ShowMsg()
{
show();
}
void show()
{
CGraph::show();
cout<<"center:"<<"("<<rx<<","<<ry<<")"<<"r"<<endl;
cout<<"digonal:"<<"("<<lx<<","<<ly<<")"<<"("<<wx<<","<<wy<<")"<<endl;
cout<<"message:"<<ccrwords<<endl;
}
virtual void save(fstream &file)
{
file<<rx<<" "<<ry<<" "<<r<<" "<<wx<<" "<<wy<<" "<<ccrwords<<" "<<color;
}
~CCircleRect()
{
if(ccrwords!=NULL)
{
delete[] ccrwords;
ccrwords=NULL;
}

}
friend class CPPage;
};
class CPPage
{
private:
CGraph *CR;
CGraph *CC;
CGraph *CCR;
int CR_num;
int CC_num;
int CCR_num;
public:
CPPage(int CRN,int CCN)
{
CR_num=CRN;
CC_num=CCN;
CR=new CRect[CRN];
CC=new CCircle[CCN];
}
CPPage()
{
CR_num=0;
CC_num=0;
CC=NULL;
CR=NULL;
}
void save(int n)
{
fstream file;
file.open("text1.txt",ios::out);
if(!file)
{
cout<<"file open error!"<<endl;
exit(1);
}
file<<n+CR_num<<endl;
int i;
for(i=0;i<CR_num;i++)
{
CR[i].save(file);
file<<endl;
}
for(i=0;i<n;i++)
{
int x1,y1,x2,y2;
cin>>x1>>y1>>x2>>y2;
file<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" ";
char a[5];
cin>>a;
file<<a<<endl;
}
file<<CC_num<<endl;
for(i=0;i<CC_num;i++)
{
CC[i].save(file);
file<<endl;
}
file<<CCR_num<<endl;
for(i=0;i<CCR_num;i++)
{
CCR[i].save(file);
if(i!=CCR_num+1)
file<<endl;
}

}
void read()
{
fstream file;
file.open("text1.txt",ios::in);
if(!file)
{
cout<<"file open error!"<<endl;
exit(1);
}
while(!file.eof())
{

file>>CR_num;
CR=new CRect[CR_num];
int i;
for(i=0;i<CR_num;i++)
{
CR[i].read(file);
}
file>>CC_num;
CC=new CCircle[CC_num];
for(i=0;i<CC_num;i++)
{
CC[i].read(file);
}
file>>CCR_num;
CCR=new CCircleRect[CCR_num];
for(i=0;i<CCR_num;i++)
{
CCR[i].read(file);
}
}
file.close();
}
void Show()
{
cout<<"Rectangle NO."<<CR_num<<endl;
int i;
for(i=0;i<CR_num;i++)
{
CR[i].ShowMsg();
}
cout<<"Circle NO."<<CC_num<<endl;
for(i=0;i<CC_num;i++)
{
CC[i].ShowMsg();
}
cout<<"CircleRect NO."<<CCR_num<<endl;
for(i=0;i<CCR_num;i++)
{
CCR[i].ShowMsg();

}
}
~CPPage()
{
if(CR!=NULL)
{

delete[] CR;
CR=NULL;
}
if(CC!=NULL)
{
delete[] CC;
CC=NULL;
}
}

};

int main(int argc, char* argv[])
{
CPPage a;
a.read();
a.Show();
int n;
cin>>n;
a.save(n);
a.read();
a.Show();
return 0;
}


然后text1.txt的内容是
3
1 2 4 5 dsfs pink 1
1 2 4 5 aaaa pink 1
1 2 4 5 dsfd pink 1
2
1 2 3 safd pink 1
1 2 3 ssss pink 1
2
1 2 3 1 2 4 5 233 pink 1
1 2 3 1 2 4 5 222 pink 1
...全文
135 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
py-python 2017-04-07
  • 打赏
  • 举报
回复
我换了一种问法,终于找到问题原因了,是多态和对象数组一起使用出现的问题
他过江 2017-04-01
  • 打赏
  • 举报
回复
看到你的结贴率别人都不想答了
赵4老师 2017-04-01
  • 打赏
  • 举报
回复
请判断每个函数调用的返回值。
赵4老师 2017-04-01
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
py-python 2017-04-01
  • 打赏
  • 举报
回复
引用 3 楼 zhao4zhong1 的回复:
需要添加seekg或rewind,我猜。
好像不是这个问题,因为我试着写了一个不引用指针的函数,循环使用的时候也崩溃了
赵4老师 2017-04-01
  • 打赏
  • 举报
回复
需要添加seekg或rewind,我猜。
赵4老师 2017-03-31
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
py-python 2017-03-31
  • 打赏
  • 举报
回复
引用 1 楼 zhao4zhong1 的回复:
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
我试过了,就是在第二次使用read函数的时候出问题的,可是为什么第一次使用就可以呢?

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

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