注意:我是C ++的新手,可能做的事是不好的做法,如果您看到这些,请告诉我,以便我可以解决,请不要太刻薄。我只是在1-2个月前才开始编码。而且我还在学习。请对我可能不了解所有事实持开放态度。
这是一个基于控制台的基于文本的游戏。效果很好!虽然,我正在其中创建一个功能,以允许用户在其上拖放任意数量的其他数据库以允许数据库传输。尽管此方法效果很好,但问题是我需要做一些处理,以尝试通过在数据库中放置数字来确保数据库中的所有信息均相同,
示例每个文件中将有2个配置文件1。它们都被命名为main。然后,用户将第二个数据库拖到游戏上,并将该数据库加载到原始数据库中。但是现在由于有2个相似的配置文件名称,因此无法区分哪个。因此,它通过一个小的功能来扫描数据库,并在副本前面放置一个数字。从5开始并逐步提高。尽管这似乎可行并且实际上并不难,但我遇到了一个问题,而且我不知道出什么问题了。我确实知道这与扫描重复项有关。请帮忙。
我整天都在尝试不同的方法或重写代码。 Google并没有向我透露太多信息。
我在代码中使用以下库。 (某些示例可能未使用,但我不记得是哪个直接在THIS函数中使用。)
#include <iostream>
#include <iterator>
#include <cstring>
#include <cmath>
#include <cassert>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <utility>
#include <vector>
#include <array>
#include <functional>
#include <fstream>
这是扫描功能,可确保没有重复的配置文件。
让我解释一下会发生什么。 我做了很多在数据库中使用的变量。如您所见,数据库内部具有一定顺序。
使用文件流访问数据库。 (我有一个功能可以将用户拖放到的所有数据库与当前的配置文件和数据结合在一起,就可以了。)
数据库中的模式如下所示。
个人资料用户名100 3 0 0 0 0骑士1100 0 0
个人资料名称的健康状况等
如果您查看变量,则会看到技术订单。
void scanCopy()
{
std::string profile{ "John's_Profile" };
std::string name{ "John_Doe" };
int health{ 0 };
int damage{ 0 };
int gold{ 0 };
int exp{ 0 };
int level{ 0 };
int score{ 0 };
std::string CLASS{ "null" };
int dungeon{ 0 };
int maxHealth{ 0 };
int lives{ 0 };
int kills{ 0 };
std::ifstream in("data/database.txt");
std::vector <std::string> profiles;
int sizeOfVector{ 0 };
while (in >> profile >> name >> health >> damage >> gold >> exp >> level >> score >> CLASS >> dungeon >> maxHealth >> lives >> kills)
{
profiles.resize(sizeOfVector += 1);
profiles.at(sizeOfVector - 1) = { profile };
std::cout << profiles.at(sizeOfVector - 1) << "\n\n";
}
in.close();
for (int loop{ 0 }; loop < sizeOfVector; ++loop)
{
int compare{ loop };
for (int index{ loop }; index < sizeOfVector; ++index)
{
if (compare == index)//meaning they are like at profiles(1)and (1)
continue;
if (profiles.at(compare) == profiles.at(index))
{
std::ofstream out("data/~database.txt", std::ios::app);
in.open("data/database.txt");
int nameIndex{ 5 };
while (in >> profile >> name >> health >> damage >> gold >> exp >> level >> score >> CLASS >> dungeon >> maxHealth >> lives >> kills)
{
if (profile == profiles.at(index))
{
out << profile << nameIndex << " " << name << " " << health << " " << damage << " " << gold << " " << exp << " " << level << " " << score << " " << CLASS << " " << dungeon << " " << maxHealth << " " << lives << " " << kills << " " << std::endl; //Notice at the start profile is put into the database with an extra variable nameIndex to make its name now unique.
++nameIndex;
}
else
{
out << profile << " " << name << " " << health << " " << damage << " " << gold << " " << exp << " " << level << " " << score << " " << CLASS << " " << dungeon << " " << maxHealth << " " << lives << " " << kills << " " << std::endl;
}
}
}
}
}
in.close();
remove("data/database.txt");
in.open("data/~database.txt");
std::ofstream out("data/database.txt", std::ios::app);
//A buffer to copy everything inside a file to a string.
std::string upData((std::istreambuf_iterator<char>(in)),
(std::istreambuf_iterator<char>()));
/////
if (out)
out << upData; //putting everything in the tmp file to the file named database.txt
out.close();
in.close();
remove("data/~database.txt");
in.close();
out.close();
}
问题在于它没有完成其工作。它将用任何数字表示数字。除此之外,它似乎还会溢出。它的作用是在您拖入某些东西之后,它会假装工作。然后,不再拖动任何输入。事实是,所有内容都从用户从数据库拖动到tmp文件的文件复制而来。然后删除数据库,并将临时文件重命名为database.txt。问题在于整个扫描功能似乎无法正常工作,而且我看不到该问题。有谁知道做这样的事情的好方法或问题是什么?谢谢!
答案 0 :(得分:0)
我们真的不需要这是一个游戏的故事,用户可以执行XYZ等。请构造一个最小的示例,如最小的可复制示例。通常,通过构建它们,您自己会发现问题。 –狂暴
谢谢。我发现了问题。我发送该函数的次数太多,导致该文件被擦除,或者没有完全扫描。很难解释我所做的真实事情,因为这很容易,但我不能很好地解释。
全部。我检查并发现了该错误,然后将其发送给我在该函数上发布了太多次的函数。或时间太少。