C++中类的对象在文件中的存取问题
#include<iostream>#include<fstream>#include<string>usingnamespacestd;classKey{private...
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Key
{
private:
string keyword;
public:
Key();
string getkey();
void setkey(string NewKey);
};
string Key::getkey()
{
return keyword;
}
Key::Key(){}
void Key::setkey(string NewKey)
{
keyword = NewKey;
}
class KeyManager
{
private:
Key Karray[100];
int top;
public:
KeyManager();
~KeyManager();
void AddKey();
};
KeyManager::KeyManager()
{
Key k;
top = -1;
fstream file("keyword.txt", ios::in);
while (1)
{
file.read((char*)&k, sizeof(k));
if (!file)break;
top++;
Karray[top] = k;
}
file.close();
}
KeyManager::~KeyManager()
{
fstream file("keyword.txt", ios::out);
for (int i = 0; i <= top; i++)
{
file.write((char*)&Karray[i], sizeof(Karray[i]));
}
file.close();
}
void KeyManager::AddKey()
{
Key KW;
string keyword;
string question;
string answer;
cout << "请输入关键字:";
getline(cin, keyword);
KW.setkey(keyword);
top++;
Karray[top] = KW;
}
int main()
{
KeyManager kw;
kw.AddKey();
return 0;
}
编译没有问题,就是我一旦运行完成,存了东西在文件里,就没办法再次运行,求高手看看我这个问题出在哪里? 展开
#include<fstream>
#include<string>
using namespace std;
class Key
{
private:
string keyword;
public:
Key();
string getkey();
void setkey(string NewKey);
};
string Key::getkey()
{
return keyword;
}
Key::Key(){}
void Key::setkey(string NewKey)
{
keyword = NewKey;
}
class KeyManager
{
private:
Key Karray[100];
int top;
public:
KeyManager();
~KeyManager();
void AddKey();
};
KeyManager::KeyManager()
{
Key k;
top = -1;
fstream file("keyword.txt", ios::in);
while (1)
{
file.read((char*)&k, sizeof(k));
if (!file)break;
top++;
Karray[top] = k;
}
file.close();
}
KeyManager::~KeyManager()
{
fstream file("keyword.txt", ios::out);
for (int i = 0; i <= top; i++)
{
file.write((char*)&Karray[i], sizeof(Karray[i]));
}
file.close();
}
void KeyManager::AddKey()
{
Key KW;
string keyword;
string question;
string answer;
cout << "请输入关键字:";
getline(cin, keyword);
KW.setkey(keyword);
top++;
Karray[top] = KW;
}
int main()
{
KeyManager kw;
kw.AddKey();
return 0;
}
编译没有问题,就是我一旦运行完成,存了东西在文件里,就没办法再次运行,求高手看看我这个问题出在哪里? 展开
2个回答
展开全部
key中的keyword是string的 不能用read/write
要么改成字符数组 要么改用<< 和>>
后一种改法大致如下
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Key
{
private:
string keyword;
public:
Key();
string getkey();
void setkey(string NewKey);
};
string Key::getkey()
{
return keyword;
}
Key::Key(){}
void Key::setkey(string NewKey)
{
keyword = NewKey;
}
class KeyManager
{
private:
Key Karray[100];
int top;
public:
KeyManager();
~KeyManager();
void AddKey();
};
KeyManager::KeyManager()
{
Key k;
top = -1;
fstream file("keyword.txt", ios::in);
while (1)
{
//file.read((char*)&k, sizeof(k));
string t;
file >> t;
k.setkey(t);
if (!file)break;
top++;
Karray[top] = k;
}
file.close();
}
KeyManager::~KeyManager()
{
fstream file("keyword.txt", ios::out);
for (int i = 0; i <= top; i++)
{
//file.write((char*)&Karray[i], sizeof(Karray[i]));
file << Karray[i].getkey();
}
file.close();
}
void KeyManager::AddKey()
{
Key KW;
string keyword;
string question;
string answer;
cout << "请输入关键字:";
getline(cin, keyword);
KW.setkey(keyword);
top++;
Karray[top] = KW;
}
int main()
{
KeyManager kw;
kw.AddKey();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询