Fstream not writing to file.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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 03 March 2018 - 04:25 AM

Modi123_1 No I have not, this is actually the first time I've heard of it.
Was This Post Helpful? 0
  • +
  • -

#17 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 03 March 2018 - 04:38 AM

I got it working!

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

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

    if(openFile.is_open() && tempStorage == "")
    {
        openFile.close();

        cout<<"No save file found.....new save created\n"<<endl;

        saveData(pl, en);

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

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

        pl.loadTileSave();
        */
    }
}


Was This Post Helpful? 0
  • +
  • -

#18 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6216
  • View blog
  • Posts: 21,452
  • Joined: 05-May 12

Re: Fstream not writing to file.

Posted 03 March 2018 - 05:47 AM

View PostXrey274, on 03 March 2018 - 06:25 AM, said:

Modi123_1 No I have not, this is actually the first time I've heard of it.

This is due to the shotgun approach to learning C++ that you are taking. As we have suggested in the past, pick up a recent book with good reviews, and learn how to program in C++ in a more structured manner that builds up on core concepts and good programming practices.
Was This Post Helpful? 1
  • +
  • -

#19 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 09 March 2018 - 10:58 AM

I was just wondering how can I extract specific lines from the file. The file looks like this:

20
10
18

How can I extract each of them? I tried:

    string tempStorage = " ", str;
    ifstream openFile;

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


    while(openFile >> str)
    {
        cout<<tempStorage<< " ";
    }



and this:

    string tempStorage = " ", str;
    ifstream openFile;

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


    while(getline(openFile, str))
    {
        cout<<tempStorage<< " ";
    }



Both just repeat 20.
Was This Post Helpful? 0
  • +
  • -

#20 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 09 March 2018 - 11:16 AM

Okay so what do you expect?

You only extract one line into tempStorage so it should never change.

By the way you have a bunch of numbers why are you using a string instead of using a numeric type?

And lastly, for now, an ifstream is always an input file why are you trying to open it as an input and output file?

Jim
Was This Post Helpful? 0
  • +
  • -

#21 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 09 March 2018 - 11:46 AM

The numbers are just a placeholder. Also i just forgot to remove it thats why its still there.
Was This Post Helpful? 0
  • +
  • -

#22 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 09 March 2018 - 12:43 PM

How can I extract more that one line?
Was This Post Helpful? 0
  • +
  • -

#23 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 09 March 2018 - 01:45 PM

You're already extracting more than one line.

What book are you using to learn the language?


Jim
Was This Post Helpful? 0
  • +
  • -

#24 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 12 March 2018 - 01:53 PM

You said I am extracting just one line above. I am reading Sam's Learn C++ 8th Edition, but it has nothing to do with it. I just haven't gotten to STL and such.
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg   User is offline

  • member icon

Reputation: 5487
  • View blog
  • Posts: 17,062
  • Joined: 25-December 09

Re: Fstream not writing to file.

Posted 12 March 2018 - 02:26 PM

Quote

You said I am extracting just one line above

I said:

Quote

You only extract one line into tempStorage so it should never change.

This does not mean that you're only extracting one line. Look at your code!

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


while(getline(openFile, str))
{


Don't you see the two extractions?

Quote

but it has nothing to do with it. I just haven't gotten to STL and such.

Baloney, the C++ streams is one of the parts of the STL that is usually covered early. If your book hasn't yet covered this topic you either need to skip ahead to the chapters covering this basic system or get a book that really teaches C++.



Jim

This post has been edited by jimblumberg: 12 March 2018 - 02:31 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2