Eporting to a text file

I have built a number guessing game, and I would like to learn how to export name, score, and date into a text file, without erasing the last scores or input.


The problem with this, it only writes the first part of what I type in.
if I write,
"Hello my name is ronnie"
it will only save "Hello" into the text file.

Also, when I start the program again, it rewrites the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  ofstream myfile ("example.txt");
  if (myfile.is_open())
  {
    string TEXT;
    cin >> TEXT;
    myfile << TEXT;
    myfile.close();
    cout << "File closed and saved";
  }
  return 0;
}



this is just a test, and I have not found a correct way to do this.
Last edited on
the reason it only writes the first part is because you are using cin. try getline instead. As far as the writing goes what are you trying to do? Write over the existing name , score, and date? or Append it to a new line?
I would like to make it export what ever is imputed, for example,

lets say I have two things,
int score;
string name;

then I set them,
score = 11;
name = Winner;

I want to get here;

---TEXT FILE---
43 Ronnie //old score
92 Abby //old score
11 Winner //new score // I want to put it down a line from the last one.



In the future, I may want to organize it by high scores, but that seems a bit to complicated right now, what do you think?

P.S. I am not in a actual C++ class, I am in an independent study class where I can learn anything I want, and I chose to learn C++. So none of this is homework, I just want to learn these things... If you have any learning Advice(for C++), I would greatly appreciate anything.


Last edited on
well that is easy enough try this
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream> //std::endl
#include <fstream> //std::ofstream

int main()
{
    int score( 999 );
    std::string name( "Giblit" );
    std::ofstream out( "output.txt" );
    out << score <<  " " << name << std::endl;
    out.close(); //I don't think this is really necessary most compilers close the file automatically when done with it
    return( 0 );
}


You were just missing the new line after the output =p
Last edited on
Just a few questions;

I see a lot of people not using "using namespace std;", instead, they are
adding std:: , whats the difference? and what shoudl I be doing?

Your program is simple, although, it has the same problem as my program has; It over writes the textfile.

I have thought about learning how to read a text file, and then store the information in the text file, into the program, so it would be saved as, old stuff in the text file + new stuff I want to save with the old stuff.
Is this teh only way? there must benother way.
Oh whoops forgot to put the append change line 8 to
1
2
3
4
5
6
std::ofstream out( "output.txt" | ios::app );
//or
std::ofstream out( "output.txt" , ios::out | ios::app );
/**Forgot which one works but one of them should**/
//ios::app means to append to end of file
//This website should have a basic i/o with files section 
out is always set for ofstream objects (even if explicitly not set in argument mode).

From:
http://www.cplusplus.com/reference/fstream/ofstream/open/#Parameters

So both of the above forms will work, the former being that little a bit more succinct (as it's an ofstream, you already know it's out!)

Andy
Last edited on
Topic archived. No new replies allowed.