【发布时间】:2014-09-30 11:03:03
【问题描述】:
我正在开发我的游戏,它将用户数据保存在设备上的某些文件中。游戏崩溃后,用户不会检索这些数据。崩溃后,相应的文件立即为空。此外,在下方创建了一个备份文件来保存新数据,以确保不删除旧数据。这个备份文件也是空的。
我不明白他们怎么可能是空的。 您可以在保存功能下方找到:
算法形式:
copy file to file_backup
if save data to file_temp then
move file_temp to file
else
delete file_temp
C++ 形式:
const std::string path = "dataPlayer.dat";
const std::string backupPath = atPath + "_back";
if (PlatformInterface::fileExists(path))
{
if (PlatformInterface::fileExists(backupPath))
PlatformInterface::removeFile(backupPath);
PlatformInterface::copyFile(path, backupPath);
}
const std::string tempPath = path + "_temp";
// Save in compact form.
XMLError error = dataDocument.SaveFile(tempPath.c_str(), true);
if (error != XML_NO_ERROR)
{
PlatformInterface::removeFile(tempPath);
}
else
{
bool moved = PlatformInterface::moveFile(tempPath, path);
if (!moved)
{
PlatformInterface::removeFile(tempPath);
}
}
如果你有任何想法,我会感谢你!
编辑: PlatformInterface::copyFile(path, backupPath) 方法:
public static void copyFile(final String path, final String destination) {
try {
if (!new File(path).exists())
return;
InputStream in = new FileInputStream(path);
OutputStream out = new FileOutputStream(destination);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
文件似乎已正确关闭。
【问题讨论】:
-
重要的是:它为什么会崩溃以及如何防止这种情况..
-
崩溃可能是由于应用程序错误,但也可能是由于设备关机(电池电量低,设备严重故障)。
标签: android c++ android-ndk