C++ 我无法使用 fstream 变量保存到文件
C++ I can't use an fstream variable to save to a file
有人能认出在这里使用fstream变量fFile的错误吗?因为在函数Save()中,写入磁盘进程(通过函数WriteTo())总是失败。但如果我声明新的本地fstream变量而不是fFile,那么保存就可以了。(请参阅下面的部分代码)
谢谢
class CardCollection{
public:
CardCollection();
int Open(const char filename[]);
void Close();
void Close();
int NumCards()const;
void ReportStatus()const;
void AddCards();
void DeleteCard(int cardnum);
void ShowCard(int cardnum)const;
void ChangeCard(int cardnum);
void DoFind();
void DoFindAgain();
void DoView();
private:
int GetField(int anyallowed);
void Load();
int fNumCards;
char *fFileName;
std::fstream fFile;
DynamicArray fStore;
char *fFindString;
int fFindPos;
int fSearchField;
};
int CardCollection::Open(const char filename[])
{
//Keep copy of filename
fFileName = new char[strlen(filename)+1];
strcpy(fFileName, filename);
fFile.open(fFileName, std::ios::out | std::ios::in);
if(!fFile.good())
return -1;
Load();
return 0;
}
void CardCollection::Save()
{
for(int i = fNumCards ; i > 0; i--){
RefCard *r = (RefCard*) fStore.Nth(i);
r->WriteTo(fFile); // If I declare a new fstream here
// instead of using fFile, save is ok
}
if(fFile.good()){
std::cout << "Saving completed";
}
else{
std::cout << "Saving error";
}
}
我敢打赌,在加载数据后不会重置fstream
。
将其添加到Save 的开头
fFile.clear();
fFile.seekp(0);
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- fstream库,试图创建一个变量名为(c++)的文件
- 变量 std::fstream 文件具有初始值设定项,但类型不完整 编译错误
- fstream 变量获取文本文件中的所有行
- FSTREAM类成员变量
- 使用 fstream 对象将文件中的信息存储到变量中
- 通过fstream读取文件时,存储在变量中的损坏数据..为什么?
- C++ 我无法使用 fstream 变量保存到文件
- 我可以在 fstream 中使用字符串变量作为文件名吗?C++
- c++ fstream -如何在.open()中添加变量而不是字符串
- 如何使用fstream对象作为成员变量
- 需要帮助关于通过fstream保存变量,我需要使用向量
- 最有效的读取文件到单独的变量使用fstream
- 通过变量在fstream trunk和应用程序模式之间切换
- C++:fstream::seekp 更改它无法控制的变量的值