C++的文件读取问题
#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对象的,但在读取,我不知道应该定义个什么类型来接收这些数据。
...全文
相关推荐
解决C++ fopen按行读取文件及所读取的数据问题
"这篇教程主要讨论了如何在C++中使用fopen函数按行读取文件内容,并解决了在读取过程中遇到的数据问题。"在C++编程中,`fopen`是一个非常常用的函数,用于打开文件进行读写操作。在这个问
C++中的文件输入输出
#### 六、总结通过对C++文件输入输出的学习,我们可以更加熟练地操作文件,无论是简单的文件创建和写入还是复杂的文件读取和解析,都能够得心应手。
cpp-libsndfile是一个C库用于读取和写入包含采样音频数据的文件
在C++项目中,通过cpp-libsndfile,开发者可以更专注于音频处理的逻辑,而不用过多关注底层细节,极大地提升了开发效率和代码质量。
cpp_laboratory:实验室测试我的CPP问题
在这个“cpp_laboratory-main”目录下,我们可以期待找到以下几类文件:1. **源代码文件**(.cpp):这些文件包含C++代码,用于实现特定的功能或解决特定问题。
C++读取Excel(源文件)
本示例中,我们关注的是如何使用C++来读取和操作Excel文件,主要涉及以下几个核心知识点:1.


