C++的文件读取问题

天台的故事 2013-12-24 12:38:51

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

class A
{
private:
int i;
public:
A()
{
i = 10;
}
virtual void show()
{
cout << i << endl;
}
virtual void file(ofstream &of)
{
of << i << endl;
}
};

class B:public A
{
private:
string str;
public:
B()
{
str = "yuzengyuan";
}
void show()
{
A::show();
cout << str << endl;
}
void file(ofstream &of)
{
A::file(of);
of << str << endl;
}
};

class ABFamily
{
public:
static A* FamilyAB(int i)
{
A* q;
if (i == 1)
{
q = new A();
}
else
{
q = new B();
}
return q;
}
};

int main()
{
A* p[10];
int temp;

for (int i = 0; i < 10; i++)
{
cout << "请输入1-2数字: ";
cin >> temp;
p[i] = ABFamily::FamilyAB(temp);
}

for (i = 0; i < 10; i++) //把数据向文件里输出
{
ofstream file("1.txt", ios::app);
p[i]->file(file);
file.close();
}

for (i = 0; i < 10; i++) //释放内存
{
delete p[i];
}
cout << "程序完成!\n";
return 0;
}

上面是的程序把数据写入到文件里,但我想重新读取出来,可是这里有个问题,因为是用了多态,所以写入到文件里的数据有 A对象也有B对象的,但在读取,我不知道应该定义个什么类型来接收这些数据。
...全文
233 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
兆帅 2013-12-26
  • 打赏
  • 举报
回复
读的时候根据前面的标志 不就可以判断数据类型了!?
天台的故事 2013-12-26
  • 打赏
  • 举报
回复
引用 4 楼 hanzhaoshuai 的回复:
小改了一点,给点分 vs2008

// virtual_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
class A
{
private:
    int i;
public:
    A()
    {
        i = 10;
    }
    virtual void show()
    {
        cout << i << endl;
    }
    virtual void file(ofstream &of)
    {
		of << 1<< " "<<i << endl;
    }
};
 
class B:public A
{
private:
    string str;
public:
    B()
    {
        str = "yuzengyuan";
    }
    void show()
    {
        A::show();
        cout << str << endl;
    }
    void file(ofstream &of)
    {
        //A::file(of);
        of <<2<<" "<<str << endl;
    }
};
 
class ABFamily
{
public:
    static A* FamilyAB(int i)
    {
        A* q;
        if (i == 1)
        {
           q = new A();
        }
        else 
        {
            q = new B();
        }
        return q;
    }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    A* p[10];  
    int temp;
 
    for (int i = 0; i < 10; i++)  
    {
        cout << "请输入1-2数字: ";
        cin >> temp;
        p[i] = ABFamily::FamilyAB(temp);
    }
    ofstream file("./1.txt", ios::app);
	if(file.fail())
		std::cout<<"open error!"<<std::endl;
    for (int i = 0; i < 10; i++)  //把数据向文件里输出
    {
        p[i]->file(file);
    }
	file.close();
	int pause;
	cin>>pause;
    for (int i = 0; i < 10; i++)  //释放内存
    {
        delete p[i];
    }
    cout << "程序完成!\n";
    return 0;
}

我发觉你加了个文件判断其他都没干
兆帅 2013-12-26
  • 打赏
  • 举报
回复
天台的故事 2013-12-26
  • 打赏
  • 举报
回复
引用 9 楼 hanzhaoshuai 的回复:
读的时候根据前面的标志 不就可以判断数据类型了!?
恩,我已经解决了,谢谢
derekrose 2013-12-25
  • 打赏
  • 举报
回复
真心不知道你想干什么。。。
兆帅 2013-12-25
  • 打赏
  • 举报
回复
小改了一点,给点分 vs2008

// virtual_test.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 
class A
{
private:
    int i;
public:
    A()
    {
        i = 10;
    }
    virtual void show()
    {
        cout << i << endl;
    }
    virtual void file(ofstream &of)
    {
		of << 1<< " "<<i << endl;
    }
};
 
class B:public A
{
private:
    string str;
public:
    B()
    {
        str = "yuzengyuan";
    }
    void show()
    {
        A::show();
        cout << str << endl;
    }
    void file(ofstream &of)
    {
        //A::file(of);
        of <<2<<" "<<str << endl;
    }
};
 
class ABFamily
{
public:
    static A* FamilyAB(int i)
    {
        A* q;
        if (i == 1)
        {
           q = new A();
        }
        else 
        {
            q = new B();
        }
        return q;
    }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    A* p[10];  
    int temp;
 
    for (int i = 0; i < 10; i++)  
    {
        cout << "请输入1-2数字: ";
        cin >> temp;
        p[i] = ABFamily::FamilyAB(temp);
    }
    ofstream file("./1.txt", ios::app);
	if(file.fail())
		std::cout<<"open error!"<<std::endl;
    for (int i = 0; i < 10; i++)  //把数据向文件里输出
    {
        p[i]->file(file);
    }
	file.close();
	int pause;
	cin>>pause;
    for (int i = 0; i < 10; i++)  //释放内存
    {
        delete p[i];
    }
    cout << "程序完成!\n";
    return 0;
}

jiandingzhe 2013-12-25
  • 打赏
  • 举报
回复
你需要在文件里标明对象的类型,比如用一个字节,如果是0就是类型A,1就是类型B,2就是类型C。
赵4老师 2013-12-25
  • 打赏
  • 举报
回复
XML
熊熊大叔 2013-12-24
  • 打赏
  • 举报
回复
这是你文件内容定义的问题. 你应该先写入一个对象类型, 再写入对象内容. 这样读的时候就可以先读出类型, 再判断如何读. 如果文件中一定不能加对象类型, 那就不能整体读出对象, 只能一个个字符读出来, 然后再判断是什么对象类型.
sublimepan 2013-12-24
  • 打赏
  • 举报
回复
增加个标记类型,先读出类型再决定创建对象
天台的故事 2013-12-24
  • 打赏
  • 举报
回复
没人吗?????

64,646

社区成员

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

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