65,208
社区成员
发帖
与我相关
我的任务
分享#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
class Date
{
public:
Date(){};
Date(int y,int m,int d):year(y),month(m),day(d){};
int year;
int month;
int day;
};
void open_file(fstream &in,const string &filename)
{
in.close();
in.clear();
in.open(filename.c_str(),ios::in);
}
int main()
{
Date date1(2014,5,28),date2(2014,5,29);
fstream in;
string filename="1.txt";
fstream writefile(filename.c_str(),ios::out);
writefile.write((char*)(&date1),sizeof(Date));
writefile.write((char*)(&date2),sizeof(Date));
writefile.close();
writefile.clear();
open_file(in,filename);
Date date3;
while(!in.eof())
{
in.read((char*)(&date3),sizeof(Date));
cout<<date3.year<<date3.month<<date3.day<<endl;
}
return 0;
}不要使用
while (条件)
更不要使用
while (组合条件)
要使用
while (1) {
if (条件1) break;
//...
if (条件2) continue;
//...
if (条件3) return;
//...
}
因为前两种写法在语言表达意思的层面上有二义性,只有第三种才忠实反映了程序流的实际情况。
典型如:
下面两段的语义都是当文件未结束时读字符
while (!feof(f)) {
a=fgetc(f);
//...
b=fgetc(f);//可能此时已经feof了!
//...
}
而这样写就没有问题:
while (1) {
a=fgetc(f);
if (feof(f)) break;
//...
b=fgetc(f);
if (feof(f)) break;
//...
}
类似的例子还可以举很多。
while(in.read((char*)(&date3),sizeof(Date)))
{
cout<<date3.year<<date3.month<<date3.day<<endl;
}
你理解里面的原理吗?// test.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Date
{
public:
Date(){};
Date(int y,int m,int d):year(y),month(m),day(d){};//构造函数
int year;
int month;
int day;
};
void open_file(fstream &in,const string &filename)//open the file
{
in.close();
in.clear();
in.open(filename.c_str(),ios::in);//以读的方式打开文件
}
int main()
{
Date date1(2014,5,28),date2(2014,5,29);//实例化Date类的两个对象date1,date2
fstream in;//fstream也是一个类,实例化对象in
string filename="1.txt";//新建一个文件1.txt用于存放数据
fstream writefile(filename.c_str(),ios::out);//以写的方式打开,不存在则创建
writefile.write((char*)(&date1),sizeof(Date));//date1数据写入1.txt
writefile.write((char*)(&date2),sizeof(Date));//date1数据写入2.txt
writefile.close();//关闭文件
writefile.clear();//清楚文件内容
open_file(in,filename);//打开文件名为filename
Date date3;//实例化Date类第三个对象date3
while(!in.eof())
{
in.read((char*)(&date3),sizeof(Date));
cout<<date3.year<<date3.month<<date3.day<<endl;
}
return 0;
}// test.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Date
{
public:
Date(){cout<<"call1"<<endl;};
Date(int y,int m,int d):year(y),month(m),day(d){cout<<"call2"<<endl;};//构造函数
int year;
int month;
int day;
};
void open_file(fstream &in,const string &filename)//open the file
{
//in.close();
//in.clear();
in.open(filename.c_str(),ios::in);//以读的方式打开文件
}
int main()
{
Date date1(2014,5,28),date2(2014,5,29);//实例化Date类的两个对象date1,date2
fstream in;//fstream也是一个类,实例化对象in
string filename="1.txt";//新建一个文件1.txt用于存放数据
fstream writefile(filename.c_str(),ios::out);//以写的方式打开,不存在则创建
writefile.write((char*)(&date1),sizeof(Date));//date1数据写入1.txt
writefile.write((char*)(&date2),sizeof(Date));//date1数据写入2.txt
writefile.close();//关闭文件
writefile.clear();//清楚文件内容
open_file(in,filename);//打开文件名为filename
Date date3;//实例化Date类第三个对象date3,利用Date()构造函数生成,但并未对成员 year,month,day做处理(赋值)
while(!in.eof())//
{
in.read((char*)(&date3),sizeof(Date));
cout<<date3.year<<date3.month<<date3.day<<endl;
}
return 0;
}