Fstream not writing to file.

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 701 Views - Last Post: 12 March 2018 - 02:26 PM Rate Topic: -----

#1 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Fstream not writing to file.

Posted 01 March 2018 - 09:50 AM

After a file called save.txt is created I insert some text into it, but when I open the file it's empty and I don't know why.(Thats in a function called loadData)

main.cpp
#include "main.h"
#include "resource.h"


//TODO Add stats which change attacks and defenses
//TODO Add an inventory
//TODO Experiment with data saving
//TODO Add music
//TODO Add player death
//TODO Make Death Screen appear after animation is over

using namespace std;



int main()
{
    bool dataAvalible = false;
    Player pl;
    Enemy en;
    string player_name;

    cout<<"                          Welcome to MortalKombat 2.\n\n"<<endl;
    cout<<"                   "; loadData(dataAvalible, pl, en);
    cout<<"                          Press any key to continue.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n\n Made By: Xrey274\n"<<endl;
    cout<<" Running Version: Alpha 2.9.1\n"<<endl;
    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
    cout<<"                                  Update Info\n"<<endl;
    cout<<" -Now the player has XP, which will be used in the future."<<endl;
    cout<<" -Code was further optimized."<<endl;
    cout<<" -Enemies can now die, but you can encounter new ones."<<endl;
    cout<<" -Better enemy encountering."<<endl;
    cout<<" -Random enemy names."<<endl;
    cout<<" -Bug fixed regarding animations and others\n\n"<<endl;

    askForePress_clearScreen();

    cout<<"                            Please enter your name:\n\n"<<endl;
    cout<<"                                   ";cin>>player_name;

    Engine(player_name, pl, en);
}

void Engine(string player_name, Player& pl, Enemy& en)
{
    bool game_over = false;
    Animations an;

    while(!game_over)
    {
        system("cls");
        if(pl.getTile() == 69)
        {
            menu_dank(game_over, player_name, en);
        }
        else
        {
            menu(game_over, player_name, pl, en);
        }


        string input_string;
        int input_number = 0;

        do
        {
            input_number = 0;
            cin >> input_string;

            stringstream converter;
            converter << input_string;
            converter >> input_number;

            if(input_number < 1  || input_number > 4)
            {
                cout<<"Error please try input again!"<<endl;
            }
        }while(!(input_number > 0 && input_number < 5));


        switch(input_number)
        {
            case 1:
                system("cls");
                Movement(pl, en);
                break;
            case 2:
                system("cls");
                Attack(player_name, pl, en, an);
                break;
        }

        if(game_over)
        {
            Death();
        }
    }
}

void menu(int game_over, string player_name, Player& pl, Enemy& en)
{
    string str1 = "'s Health: ", str2 = "          ",  healthS, enemyHealthS;
    int totalLen;

    cout<<"                      Please choose what you want to do.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;
    cout<<"\n 1-Move\n"<<endl;
    cout<<" 2-Attack\n"<<endl;
    cout<<" 3-Inventory\n"<<endl;
    cout<<" 4-Stats\n"<<endl;

    stringstream stringToInt;
    stringToInt << pl.getHealth();
    stringToInt >> healthS;

    stringstream stringToInt2;
    stringToInt2 << en.getHealth();
    stringToInt2 >> enemyHealthS;

    cout<<" "<<player_name<<str1<<healthS;

    if(en.getEncounter() == true)
    {
        totalLen = player_name.length() + en.getNameLength() + str1.length() + str2.length() + healthS.length() + enemyHealthS.length() + 1;

        for(int i = 0; i < 66 - totalLen ; i++)
        {
            str2.insert(0, " ");
        }

        cout<<str2<<en.getName()<<str1<<enemyHealthS<<endl;
    }
    else
    {
        cout<<""<<endl;
    }

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}

void menu_dank(int game_over, string player_name, Enemy& en)
{
    cout<<"                      69696969696969696969696969696969696969.\n"<<endl;
    cout<<"--------------------------------------------------------------------------------"<<endl;

    cout<<"\n 1-6969\n"<<endl;
    cout<<" 2-696969\n"<<endl;
    cout<<" 3-6969696969\n"<<endl;
    cout<<" 4-69696969\n"<<endl;
    cout<<" 69696969:"<<player_name<<endl;

    cout<<"\n\n--------------------------------------------------------------------------------"<<endl;
}


void Movement(Player& pl, Enemy& en)
{
    bool loopOver = false;
    do
    {
        cout<<"                      How many tiles do you want to move?"<<endl;
        cout<<"\n\n                                    Tile:"<<pl.getTile()<<"\n"<<endl;
        cout<<"\n--------------------------------------------------------------------------------"<<endl;
        cout<<"\n\n                    Hint:You can only move 5 tiles at a time."<<endl;

        if(en.getEncounter() == false)
        {
            int movementInput = 0;
            cin>>movementInput;

            if(movementInput > 0 && movementInput < 6)
            {
                int x = 6 - movementInput;

                for(int i = x; i <= 5; i++)
                {
                    pl.tileIncrease();
                    en.Encountering(pl.getTile());
                }

                if(en.getEncounter() == true)
                {
                    loopOver = true;
                    break;
                    askForPress();
                }
            }
            else if(movementInput == -1)
            {
                loopOver = true;
                break;
            }
            else
            {
                cout<<"Error, try again"<<endl;
                continue;
            }

        }

        else if(en.getEncounter() == true)
        {
            cout<<"\n                  Sorry, but you can't move while in a fight!"<<endl;
            loopOver = true;

            askForPress();
        }

        clearScreen();
    }while(loopOver == false);

}

void Attack(string player_name, Player& pl, Enemy& en, Animations& an)
{
    bool loopOver = false;
    string str1 = "'s Health: ", str2 = "          ", healthS, enemyHealthS;
    int totalLen;

    do
    {
        cout<<"                            Please choose an attack!"<<endl;

        cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

        cout<<" 1.Hadouken\n"<<endl;
        cout<<" 2.Recover\n"<<endl;
        cout<<" 3.Might Guy\n"<<endl;
        cout<<" 4.Toxic Spikes"<<endl;


        cout<<"\n\n--------------------------------------------------------------------------------"<<endl;

        stringstream stringToInt;
        stringToInt << pl.getHealth();
        stringToInt >> healthS;

        stringstream stringToInt2;
        stringToInt2 << en.getHealth();
        stringToInt2 >> enemyHealthS;

        totalLen = player_name.length() + en.getNameLength() + str1.length() + str2.length() + healthS.length() + enemyHealthS.length() + 1;

        for(int i = 0; i < 66 - totalLen ; i++)
        {
            str2.insert(0, " ");
        }

        cout<<" "<<player_name<<str1<<healthS<<str2<<en.getName()<<str1<<enemyHealthS<<endl;

        string attackString;
        int attackInput = 0;

        if(en.getEncounter() == true)
        {
            cin>>attackString;

            stringstream converter;
            converter << attackString;
            converter >> attackInput;
        }
        else if(en.getEncounter() == false)
        {
            cout<<"\n\n                 You can't attack without encountering an enemy!"<<endl;
            loopOver = true;

            break;
            askForPress();
        }

        switch(attackInput)
        {
            case -1:
                loopOver = true;
                break;
            case 1:
                en.take_Damage(5, pl);
                an.hadouken_Animation();
                en.Attack(pl);
                break;
            case 2:
                pl.setHealth(1);
                an.recover_Animation();
                break;
            case 3:
                pl.mightGuy(en, pl);
                an.hadouken_Animation();
                break;
            default:
                cout<<"Error, try again."<<endl;
                break;
        }

    }while(loopOver == false);
}

void Death()
{

}

int saveData()
{

}

bool loadData(int dataAvalible, Player& pl, Enemy& en)
{
    string tempStorage;

    fstream openFile;
    openFile.open("save.txt", fstream::in | fstream::out | fstream::trunc);
    openFile >> tempStorage;

    if(tempStorage == "")
    {
        cout<<"No save file found.....new save created\n"<<endl;

        tempStorage = "20";

        openFile<<"HELLO";
        cout<<

        openFile.flush();
        openFile.close();
    }
   /* else
    {
        pl.loadHealthSave();
        en.loadHealthSave();

        pl.loadTileSave();
    }
*/

}

void askForPress()
{
    cin.get();
    cin.sync();
}

void clearScreen()
{
    system("cls");
}

void askForePress_clearScreen()
{
    cin.get();
    cin.sync();

    system("cls");
}




Is This A Good Question/Topic? 0
  • +

Replies To: Fstream not writing to file.

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Fstream not writing to file.

Posted 01 March 2018 - 09:52 AM

Your 'save data' seems suspiciously empty.

304  int saveData()
305  {
306 
307  }


Was This Post Helpful? 0
  • +
  • -

#3 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 09:56 AM

For now.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 09:57 AM

bool loadData(int dataAvalible, Player& pl, Enemy& en)
{
    string tempStorage;

    fstream openFile;
    openFile.open("save.txt", fstream::in | fstream::out | fstream::trunc);
    openFile >> tempStorage;

    if(tempStorage == "")
    {
        cout<<"No save file found.....new save created\n"<<endl;

        tempStorage = "20";

        openFile<<"HELLO";
        cout<<

        openFile.flush();
        openFile.close();
    }
   /* else
    {
        pl.loadHealthSave();
        en.loadHealthSave();

        pl.loadTileSave();
    }
*/

}



Do you understand what the trunc flag means?
Was This Post Helpful? 0
  • +
  • -

#5 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:05 AM

Oh..

Even after I removed it still doesn't write to the file.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:08 AM

Well you may want to check if the file actually opened instead of trying to retrieve something from a file that didn't open.

And by the way you didn't answer my question.


Jim
Was This Post Helpful? 0
  • +
  • -

#7 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:15 AM

Now I know. Also how can I chech if the file opened?

check*
Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:19 AM

To check if a file opened you need to check the stream state.


Jim
Was This Post Helpful? 0
  • +
  • -

#9 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:25 AM

I tried with is_opened on a loop, but still nothing happens.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:28 AM

So what exactly is this "is_opened" you're talking about?

Have you actually read any documentation for that standard class you're trying to use?

Jim
Was This Post Helpful? 0
  • +
  • -

#11 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:33 AM

fstream:is_open is what I am talking about. And no I have not.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 10:51 AM

You need to show the code where you tried to check the stream state.

Quote

And no I have not.

Then maybe it's time you did.

Jim
Was This Post Helpful? 1
  • +
  • -

#13 Xrey274   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 230
  • Joined: 10-December 16

Re: Fstream not writing to file.

Posted 01 March 2018 - 03:11 PM

This is the code:
bool loadData(int dataAvalible, Player& pl, Enemy& en)
{
    string tempStorage;
    fstream openFile;

    do
    {
        openFile.open("save.txt", fstream::in | fstream::out);
        openFile >> tempStorage;

        if(openFile.is_open() && tempStorage == "")
        {
            cout<<"No save file found.....new save created\n"<<endl;

            tempStorage = "20";

            openFile<<tempStorage;

            //openFile.close();
        }
   /* else
    {
        pl.loadHealthSave();
        en.loadHealthSave();

        pl.loadTileSave();
    }


*/

    }while(!openFile.is_open());

}



Also why would opening the file fail?
Was This Post Helpful? 0
  • +
  • -

#14 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14097
  • View blog
  • Posts: 56,497
  • Joined: 12-June 08

Re: Fstream not writing to file.

Posted 01 March 2018 - 03:21 PM

A couple of things. Why would you surround that in a do-while loop?

Have you considered putting a try/catch to see what the exception says?
Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg   User is online

  • member icon

Reputation: 5491
  • View blog
  • Posts: 17,068
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 01 March 2018 - 03:38 PM

Also note that checking if the file opened after you try to read the file is pointless.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2