一段C++文件读取的代码问题
#include<iostream.h>#include<fstream.h>#include<stdlib.h>#include<string.h>classInorO...
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
class InorOut
{ public:
double In;
double Out;
char Date[9];
void Display();
};
void InorOut::Display()
{ cout<<Date<<"\t\t"<<In<<"\t\t";
cout<<Out<<"\t\t"<<In - Out;
cout<<"\t\t";
}
void Show(InorOut *CurUsrInc)
{ InorOut *AllInc;
fstream f("0000.sav",ios::binary|ios::in|ios::out);
if(!f)
{cerr<<"文件不能打开"<<endl;
return;
}
f.seekg(0,ios::end);
long posEnd=f.tellg();
f.seekg(0,ios::beg);
do
{f.read((char*)&AllInc,sizeof(InorOut));
}while(f.tellg()!=posEnd);
}
void main()
{ InorOut *CurUsrInc;
CurUsrInc = (InorOut*)malloc(sizeof(InorOut));
fstream iof("0000.sav",ios::binary|ios::in|ios::out);
iof.seekp(0,ios::end);
if(!iof)
cerr<<"\n\n文件建立失败"<<endl;
CurUsrInc->In=0;
CurUsrInc->Out=0;
strcpy(CurUsrInc->Date, "01/01/09");
iof.write((char*)&CurUsrInc,sizeof(InorOut));
iof.close();
Show(CurUsrInc);
}
VC6的,上面代码运行一直自己退出,请问是什么问题
编译连接可以通过啊……但是文件的内容没有显示出来他就自己退出了 展开
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
class InorOut
{ public:
double In;
double Out;
char Date[9];
void Display();
};
void InorOut::Display()
{ cout<<Date<<"\t\t"<<In<<"\t\t";
cout<<Out<<"\t\t"<<In - Out;
cout<<"\t\t";
}
void Show(InorOut *CurUsrInc)
{ InorOut *AllInc;
fstream f("0000.sav",ios::binary|ios::in|ios::out);
if(!f)
{cerr<<"文件不能打开"<<endl;
return;
}
f.seekg(0,ios::end);
long posEnd=f.tellg();
f.seekg(0,ios::beg);
do
{f.read((char*)&AllInc,sizeof(InorOut));
}while(f.tellg()!=posEnd);
}
void main()
{ InorOut *CurUsrInc;
CurUsrInc = (InorOut*)malloc(sizeof(InorOut));
fstream iof("0000.sav",ios::binary|ios::in|ios::out);
iof.seekp(0,ios::end);
if(!iof)
cerr<<"\n\n文件建立失败"<<endl;
CurUsrInc->In=0;
CurUsrInc->Out=0;
strcpy(CurUsrInc->Date, "01/01/09");
iof.write((char*)&CurUsrInc,sizeof(InorOut));
iof.close();
Show(CurUsrInc);
}
VC6的,上面代码运行一直自己退出,请问是什么问题
编译连接可以通过啊……但是文件的内容没有显示出来他就自己退出了 展开
2个回答
展开全部
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
class InorOut
{
public:
double In;
double Out;
char Date[9];
void Display();
};
void InorOut::Display()
{
cout<<Date<<"\t\t"<<In<<"\t\t";
cout<<Out<<"\t\t"<<In - Out;
cout<<"\t\t";
}
void Show(InorOut *CurUsrInc)
{
InorOut *AllInc = new InorOut; //new instance
fstream f("0000.sav",ios::binary|ios::in); /////////////////////open method
if(!f)
{
cerr<<"文件不能打开"<<endl;
return;
}
f.seekg(0,ios::end);
long posEnd=f.tellg();
f.seekg(0,ios::beg);
do
{
f.read((char*)AllInc,sizeof(InorOut)); //no &
}while(f.tellg()!=posEnd);
cout<<"After Reading"<<endl;//print result
cout << AllInc->In<<endl;
cout << AllInc->Out<<endl;
cout << AllInc->Date<<endl;
delete AllInc;//release instance
}
void main()
{
InorOut *CurUsrInc;
CurUsrInc = new InorOut; //new instance
fstream iof("0000.sav",ios::binary|ios::out); /////////write method
iof.seekp(0,ios::end);
if(!iof)
cerr<<"\n\n文件建立失败"<<endl;
CurUsrInc->In=0;
CurUsrInc->Out=0;
strcpy(CurUsrInc->Date, "01/01/09");
iof.write((char*)CurUsrInc,sizeof(InorOut)); //no &
iof.close();
Show(CurUsrInc);
delete CurUsrInc;//release instance
}
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
class InorOut
{
public:
double In;
double Out;
char Date[9];
void Display();
};
void InorOut::Display()
{
cout<<Date<<"\t\t"<<In<<"\t\t";
cout<<Out<<"\t\t"<<In - Out;
cout<<"\t\t";
}
void Show(InorOut *CurUsrInc)
{
InorOut *AllInc = new InorOut; //new instance
fstream f("0000.sav",ios::binary|ios::in); /////////////////////open method
if(!f)
{
cerr<<"文件不能打开"<<endl;
return;
}
f.seekg(0,ios::end);
long posEnd=f.tellg();
f.seekg(0,ios::beg);
do
{
f.read((char*)AllInc,sizeof(InorOut)); //no &
}while(f.tellg()!=posEnd);
cout<<"After Reading"<<endl;//print result
cout << AllInc->In<<endl;
cout << AllInc->Out<<endl;
cout << AllInc->Date<<endl;
delete AllInc;//release instance
}
void main()
{
InorOut *CurUsrInc;
CurUsrInc = new InorOut; //new instance
fstream iof("0000.sav",ios::binary|ios::out); /////////write method
iof.seekp(0,ios::end);
if(!iof)
cerr<<"\n\n文件建立失败"<<endl;
CurUsrInc->In=0;
CurUsrInc->Out=0;
strcpy(CurUsrInc->Date, "01/01/09");
iof.write((char*)CurUsrInc,sizeof(InorOut)); //no &
iof.close();
Show(CurUsrInc);
delete CurUsrInc;//release instance
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询