c++文件操作 问什么是3个Date对象的数据,应该是2个啊????

小小白杨123 2014-05-28 11:27:23
#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;
}
...全文
191 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-06-05
  • 打赏
  • 举报
回复
不要使用
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;
 //...
}
类似的例子还可以举很多。
buyong 2014-06-05
  • 打赏
  • 举报
回复
原理就是。读到文件结束不会eof,再读一次才会
小小白杨123 2014-05-31
  • 打赏
  • 举报
回复
引用 5 楼 buyong 的回复:
while(!in.eof())// { in.read((char*)(&date3),sizeof(Date)); cout<<date3.year<<date3.month<<date3.day<<endl; } 改成 while(1) { in.read((char*)(&date3),sizeof(Date)); if(in.eof()) break; cout<<date3.year<<date3.month<<date3.day<<endl; }
我知道怎么改,除了你那种改法,我还可以这么做
while(in.read((char*)(&date3),sizeof(Date)))
    {
        cout<<date3.year<<date3.month<<date3.day<<endl;
    }
你理解里面的原理吗?
FeelTouch Labs 2014-05-29
  • 打赏
  • 举报
回复
// 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;
}
xiaohuh421 2014-05-29
  • 打赏
  • 举报
回复
1. 查看1.txt这个文件的大小. 或者使用hex工具以16进制方式查看文件内容. 看到底有几个Date的空间. 2. in.read((char*)(&date3),sizeof(Date)); 这里要保证read返回了你想读取的字节数量.
小小白杨123 2014-05-29
  • 打赏
  • 举报
回复
引用 2 楼 FENGQIYUNRAN 的回复:
// 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;
}
你确定33行的注释是正确的?我知道会调用三次构造函数,我只是往1.txt的文件中写入了两个Date对象的数据,为什么会输出三个对象的数据呢?
FeelTouch Labs 2014-05-29
  • 打赏
  • 举报
回复
// 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;
}

buyong 2014-05-29
  • 打赏
  • 举报
回复
while(!in.eof())// { in.read((char*)(&date3),sizeof(Date)); cout<<date3.year<<date3.month<<date3.day<<endl; } 改成 while(1) { in.read((char*)(&date3),sizeof(Date)); if(in.eof()) break; cout<<date3.year<<date3.month<<date3.day<<endl; }

65,208

社区成员

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

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