Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用ofstream C++;_C++_File_Line_Overwrite - Fatal编程技术网

C++ 用ofstream C++;

C++ 用ofstream C++;,c++,file,line,overwrite,C++,File,Line,Overwrite,我正在做一个小游戏,我正在把玩家的详细信息保存在一个txt文件中。 该txt文件的示例: Eric 13 8 10 10 30 10 10 50 0 0 0 0 William 1 0 10 30 30 10 10 50 0 0 0 0 John 1 0 10 30 30 10 10 50 0 0 0 0 这就是我的想法:当玩家选择在玩游戏时保存游戏时,save\u game功能应该检查是否已经保存了任何数据。如果有,它应该覆盖该特定行,而不是将数据追加到txt的末尾 以下是我目前的职能: /

我正在做一个小游戏,我正在把玩家的详细信息保存在一个txt文件中。 该txt文件的示例:

Eric 13 8 10 10 30 10 10 50 0 0 0 0
William 1 0 10 30 30 10 10 50 0 0 0 0
John 1 0 10 30 30 10 10 50 0 0 0 0
这就是我的想法:当玩家选择在玩游戏时保存游戏时,
save\u game
功能应该检查是否已经保存了任何数据。如果有,它应该覆盖该特定行,而不是将数据追加到txt的末尾

以下是我目前的职能:

// SAVE GAME
void save_game(Player player)
{
    ofstream coutfile (SaveDestiny, ios::app);

    if (coutfile.is_open()) // if it opens correctly
    {
        // Now checking if the name already exists
        string imported_name;

        ifstream cinfile (SaveDestiny); // opens file that contains the saved games

        cinfile >> imported_name; // getting first element of file

        bool j = 0; // j = 0 while the strings don't match. j = 1 when the string was found

        while (cinfile >> imported_name) // while the end of file is not reached
        {
            if (player.name.compare(imported_name) == 0) // if the strings are the same, overwrite data
            {
                j = 1;

                coutfile << "                                                                         \r" << endl;
                break;
            }
            else // if the strings are different, keep reading
            {
                cinfile >> imported_name;
            }
        }

        // Continuing...
        coutfile << player.name << " " << player.level << " " << player.exp << " " << player.max_exp << " "
            << player.hp << " " << player.max_hp << " " << player.mp << " " << player.max_mp << " "
            << player.gold << " " << player.weapon << " " << player.shield << " " << player.heal_spell << " "
            << player.attack_spell << endl;
    }
    else
    {
        ofstream coutfile (SaveDestiny, ios::app);
        coutfile << "test";
        cout << "Unable to open file";
        cin.get();
    }

    draw_rectangle(37,8,72,14,15);  // white limits
    draw_rectangle(39,9,70,13,9);   // blue background
    cor(9,15);
    gotoxy(50,10);
    cout << "GAME SAVED!";
    gotoxy(41,12);
    cor(9,14);
    cout << "Press <Enter> to continue... ";
    cin.get();
}
// SAVE GAME
void save_game(Player player)
{
    ofstream coutfile (SaveDestiny, ios::app);

    if (coutfile.is_open()) // if it opens correctly
    {
        string imported_name;
        ifstream cinfile (SaveDestiny); // opens file that contains the saved games

        bool j = 0;

        // Now checking if the name already exists
        while (cinfile >> imported_name) // while the end of file is not reached
        {
            if (player.name.compare(imported_name) == 0) // if the strings are the same, overwrite data
            {
                j = 1; // enable overwrite
                break;
            }
            // if the strings are different, keep reading
        }
        // at this point: j = 0 to append to end. j = 1 to overwrite.

        // Overwriting data
        if (j == 1)
        {
            ifstream cinfile (SaveDestiny);

            // now determining the size of the vector (number of lines in txt)
            int line_numbers = 0;
            string line;
            while (getline(cinfile, line))
            {
                line_numbers++;
            }

            cinfile.close();    // closing
            ifstream cinfile2 (SaveDestiny);    // reopening to read from the beginning 

            // now creating the vector with the saves
            vector<vector<string>> temp_saves(line_numbers, vector<string>(2));
            string name2;
            string values;

            for (unsigned int x = 0; x < temp_saves.size(); x++)
            {
                cinfile2 >> name2;
                getline(cinfile2, values);

                temp_saves[x][0] = name2;
                temp_saves[x][1] = values;
            }

            coutfile.close(); // closing output file
            ofstream coutfile2 (SaveDestiny); // reopening in overwrite mode

            // delete all saves.txt, copying vector content to txt (except the one we want to overwrite)
            for (unsigned int x = 0; x < temp_saves.size(); x++)
            {
                if ( temp_saves[x][0].compare(player.name) != 0)
                {
                    coutfile2 << temp_saves[x][0] << temp_saves[x][1] << endl;
                }
            }
            coutfile2.close(); // closing output file
        }

        // Appending new data...
        ofstream coutfile3 (SaveDestiny, ios::app); // reopening in append mode
        coutfile3 << player.name << " " << player.level << " " << player.exp << " " << player.max_exp << " "
            << player.hp << " " << player.max_hp << " " << player.mp << " " << player.max_mp << " "
            << player.gold << " " << player.weapon << " " << player.shield << " " << player.heal_spell << " "
            << player.attack_spell << endl;
    }
    else
    {
        ofstream coutfile (SaveDestiny, ios::app);
        cout << "Unable to open file";
        cin.get();
    }

    draw_rectangle(37,8,72,14,15);  // white limits
    draw_rectangle(39,9,70,13,9);   // blue background
    cor(9,15);
    gotoxy(50,10);
    cout << "GAME SAVED!";
    gotoxy(41,12);
    cor(9,14);
    cout << "Press <Enter> to continue... ";
    cin.get();
}
//保存游戏
无效保存游戏(玩家)
{
流coutfile(SaveDestiny,ios::app);
if(coutfile.is_open())//是否正确打开
{
//现在检查名称是否已经存在
输入的字符串名称;
ifstream cinfile(SaveDestiny);//打开包含已保存游戏的文件
cinfile>>导入的_name;//获取文件的第一个元素
bool j=0;//当字符串不匹配时j=0。当找到字符串时j=1
while(cinfile>>导入的_name)//未到达文件末尾时
{
if(player.name.compare(imported_name)==0)//如果字符串相同,则覆盖数据
{
j=1;
coutfile导入的\u名称;
}
}
//继续。。。

coutfile在大多数现代文件系统中,文件不是“基于行”(或“基于记录”)的,而是基于字符的,因此不能“覆盖行”旧行可能是20个字符长,而新的一行可能是24个字符,在这种情况下,它将覆盖下一行的旧行和前4个字符。为了使这一工作,您必须在文件后面的“行”之后“推”所有的东西,这是不可能的C++(或C)IO设施。 一种选择是以固定长度(比如50个字符)写入所有行,因此覆盖第三行需要替换100到149个字符,即使该行实际上只需要24个字符


另一种选择是以基于记录的形式将文件保存在内存中,并在每次更改文件时写出整个文件(或至少写出新行及其后的所有行)

在大多数现代文件系统中,文件不是“基于行”(或“基于记录”),而是基于字符的,因此不能“覆盖行”旧行可能是20个字符长,而新的一行可能是24个字符,在这种情况下,它将覆盖下一行的旧行和前4个字符。为了使这一工作,您必须在文件后面的“行”之后“推”所有的东西,这是不可能的C++(或C)IO设施。 一种选择是以固定长度(比如50个字符)写入所有行,因此覆盖第三行需要替换100到149个字符,即使该行实际上只需要24个字符


另一种选择是以基于记录的形式将文件保存在内存中,并在每次更改文件时写出整个文件(或者至少写出新行及其后的所有行)

好的,我已经设法解决了这个问题,现在它工作得很好!:D

首先,
函数
检查玩家名称是否已经在
txt
上。我创建了一个启用变量
j
。当
j=1
时,名称存在,数据需要被
覆盖
!当
j=0
时,函数将
立即将数据追加到txt中

好的,假设
j=1
。该函数确定
txt
中的行数。然后它创建一个
向量
,其中包含两个向量:
名称
,以及
游戏变量
。 之后,该函数删除以前的
txt
文件内容。并将向量内容写入
txt
,但需要覆盖的数据除外(它将跳过将该部分写入
txt
),因为在函数结束时,新数据将被写入。:D我希望我说得足够清楚。如果有人不理解我所写的内容,请道歉

这是我新的
save\u游戏
功能:

// SAVE GAME
void save_game(Player player)
{
    ofstream coutfile (SaveDestiny, ios::app);

    if (coutfile.is_open()) // if it opens correctly
    {
        // Now checking if the name already exists
        string imported_name;

        ifstream cinfile (SaveDestiny); // opens file that contains the saved games

        cinfile >> imported_name; // getting first element of file

        bool j = 0; // j = 0 while the strings don't match. j = 1 when the string was found

        while (cinfile >> imported_name) // while the end of file is not reached
        {
            if (player.name.compare(imported_name) == 0) // if the strings are the same, overwrite data
            {
                j = 1;

                coutfile << "                                                                         \r" << endl;
                break;
            }
            else // if the strings are different, keep reading
            {
                cinfile >> imported_name;
            }
        }

        // Continuing...
        coutfile << player.name << " " << player.level << " " << player.exp << " " << player.max_exp << " "
            << player.hp << " " << player.max_hp << " " << player.mp << " " << player.max_mp << " "
            << player.gold << " " << player.weapon << " " << player.shield << " " << player.heal_spell << " "
            << player.attack_spell << endl;
    }
    else
    {
        ofstream coutfile (SaveDestiny, ios::app);
        coutfile << "test";
        cout << "Unable to open file";
        cin.get();
    }

    draw_rectangle(37,8,72,14,15);  // white limits
    draw_rectangle(39,9,70,13,9);   // blue background
    cor(9,15);
    gotoxy(50,10);
    cout << "GAME SAVED!";
    gotoxy(41,12);
    cor(9,14);
    cout << "Press <Enter> to continue... ";
    cin.get();
}
// SAVE GAME
void save_game(Player player)
{
    ofstream coutfile (SaveDestiny, ios::app);

    if (coutfile.is_open()) // if it opens correctly
    {
        string imported_name;
        ifstream cinfile (SaveDestiny); // opens file that contains the saved games

        bool j = 0;

        // Now checking if the name already exists
        while (cinfile >> imported_name) // while the end of file is not reached
        {
            if (player.name.compare(imported_name) == 0) // if the strings are the same, overwrite data
            {
                j = 1; // enable overwrite
                break;
            }
            // if the strings are different, keep reading
        }
        // at this point: j = 0 to append to end. j = 1 to overwrite.

        // Overwriting data
        if (j == 1)
        {
            ifstream cinfile (SaveDestiny);

            // now determining the size of the vector (number of lines in txt)
            int line_numbers = 0;
            string line;
            while (getline(cinfile, line))
            {
                line_numbers++;
            }

            cinfile.close();    // closing
            ifstream cinfile2 (SaveDestiny);    // reopening to read from the beginning 

            // now creating the vector with the saves
            vector<vector<string>> temp_saves(line_numbers, vector<string>(2));
            string name2;
            string values;

            for (unsigned int x = 0; x < temp_saves.size(); x++)
            {
                cinfile2 >> name2;
                getline(cinfile2, values);

                temp_saves[x][0] = name2;
                temp_saves[x][1] = values;
            }

            coutfile.close(); // closing output file
            ofstream coutfile2 (SaveDestiny); // reopening in overwrite mode

            // delete all saves.txt, copying vector content to txt (except the one we want to overwrite)
            for (unsigned int x = 0; x < temp_saves.size(); x++)
            {
                if ( temp_saves[x][0].compare(player.name) != 0)
                {
                    coutfile2 << temp_saves[x][0] << temp_saves[x][1] << endl;
                }
            }
            coutfile2.close(); // closing output file
        }

        // Appending new data...
        ofstream coutfile3 (SaveDestiny, ios::app); // reopening in append mode
        coutfile3 << player.name << " " << player.level << " " << player.exp << " " << player.max_exp << " "
            << player.hp << " " << player.max_hp << " " << player.mp << " " << player.max_mp << " "
            << player.gold << " " << player.weapon << " " << player.shield << " " << player.heal_spell << " "
            << player.attack_spell << endl;
    }
    else
    {
        ofstream coutfile (SaveDestiny, ios::app);
        cout << "Unable to open file";
        cin.get();
    }

    draw_rectangle(37,8,72,14,15);  // white limits
    draw_rectangle(39,9,70,13,9);   // blue background
    cor(9,15);
    gotoxy(50,10);
    cout << "GAME SAVED!";
    gotoxy(41,12);
    cor(9,14);
    cout << "Press <Enter> to continue... ";
    cin.get();
}
//保存游戏
无效保存游戏(玩家)
{
流coutfile(SaveDestiny,ios::app);
if(coutfile.is_open())//是否正确打开
{
输入的字符串名称;
ifstream cinfile(SaveDestiny);//打开包含已保存游戏的文件
boolj=0;
//现在检查名称是否已经存在
while(cinfile>>导入的_name)//未到达文件末尾时
{
if(player.name.compare(imported_name)==0)//如果字符串相同,则覆盖数据
{
j=1;//启用覆盖
打破
}
//如果字符串不同,请继续阅读
}
//此时:j=0追加到end。j=1覆盖。
//覆盖数据
如果(j==1)
{
ifstream cinfile(SaveDestiny);
//现在确定向量的大小(txt中的行数)
整数行数=0;
弦线;
while(getline(cinfile,line))
{
行数++;
}
cinfile.close();//正在关闭
ifstream cinfile2(SaveDestiny);//重新打开以从头开始读取
//现在使用保存创建向量
向量温度保存(行号,向量(2));
字符串名称2;
字符串值;
for(无符号整数x=0;x>名称2;
getline(cinfile2,值);
临时保存[x][0]=name2;
temp_保存[x][1]=值;
}
coutfile.close();//关闭输出文件
ofstream coutfile2(SaveDestiny);//以覆盖模式重新打开
//删除所有saves.txt,将矢量内容复制到