1~2
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
typedef struct { string name,num;
double Buy,Sell,Deal;
}Node;
Node node[4];
int main()
{ //输出该编译器的文件。
/*ifstream fin("D:\\编程文件\\main.cpp",ios_base::in);
if(fin)
{ char ch;
while((ch=fin.get())!=EOF)
cout.put(ch);
}
fin.close();
*/
ofstream file("c:\\stock.dat",ios_base::out|ios_base::binary);
if(!file)
cerr<<"open error!!!"<<endl,abort(); //结束程序
for(int i=0;i<4;++i)
{cin>>node[i].name>>node[i].num>>node[i].Buy>>node[i].Sell>>node[i].Deal;
file.write(reinterpret_cast<char*>(&node[i]),sizeof(node[i]));
}
file.close();
ifstream f("c:\\stock.dat",ios_base::in|ios_base::binary);
while(!f.eof())//判断是不是到文件的结尾
{ Node s;
f.read(reinterpret_cast<char*>(&s),sizeof(s));
cout<<s.name<<" "<<s.num<<" "<<s.Buy<<" "<<s.Sell<<" "<<s.Deal<<endl;
}
f.close();
system("pause");
return 0;
}
3,,这个实验做的有点纠结,,,,,,,,
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
ofstream file;
class MailMan
{public:
bool bHaveMsg ; //是否有信息的标记
void OutMsg(); //显示用户输入的信息
void ReadMsgFromFile(); //从文件中读取信息
void SaveMsgToFile(); //向文件中保存信息
void InMsg(); //接收用户的信息
MailMan();
virtual ~MailMan() {}
private:
char Buf[1024];
fstream fs ;
};
MailMan::MailMan()
{ memset(Buf,'\0',sizeof(Buf));
fs.open("C:\\msg.ini",ios_base::in|ios_base::binary);
fs.close();
bHaveMsg=false;
}
void MailMan::SaveMsgToFile()
{ file.open("C:\\msg.ini",ios_base::app|ios_base::binary);
file.write(reinterpret_cast<char*>(&Buf),sizeof(Buf));
file.close();
bHaveMsg=true;
}
void MailMan::InMsg()
{ cin>>Buf;
cout<<endl;
}
void MailMan:: ReadMsgFromFile()
{ fs.open("C:\\msg.ini",ios_base::in|ios_base::binary);
char ch;
/*while((ch=fs.get())!=EOF)
cout.put(ch);
cout<<endl;
fs.close();
*/
while(!fs.eof())
{ while((ch=fs.get())!=EOF)
cout.put(ch);
}
fs.close();
}
void MailMan::OutMsg()
{ ReadMsgFromFile();
InMsg();
SaveMsgToFile();
}
int main()
{ file.open("C:\\msg.ini",ios_base::app|ios_base::binary);
char ch[100];
cin>>ch;
file.write(reinterpret_cast<char*>(&ch),sizeof(ch));
file.close();
MailMan a;
a.OutMsg();
a.OutMsg();
system("pause");
}