Writing to and reading from a file

I want to create a highscore system, how can i write to the file and save the input, and how to i output the data on the text file if the user selects the 2nd option?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{
	
	while (true)
	{
		cout << "Welcome to the highscore leaderboard. Please select an option from the menu\n1. Enter a score\n2. Display Scores\n3. Exit" << endl;
		int option, x;
		cin >> option;
		ofstream myfile;
		ifstream writefile;

		if (option == 1)
		{
			
			myfile.open("highscores.txt");
			writefile.open("highscores.txt");
			myfile << "Top Ten Highscores.\n";

			cout << "You have chosen '1.' Please enter how many scores you want to enter!" << endl;
			int numofscore;
			cin >> numofscore;
			cout << "Enter your " << numofscore << " scores!" << endl;
			for (int i = 0; i < numofscore; i++)
			{
				cin >> x;
				myfile << x << "\n";
				writefile >> x;
			}
			
			

			cout << "Your number has been entered!" << endl;
			
			myfile.close();
			writefile.close();

			continue;
		}
		else if (option == 2)
		{
			writefile.open("highscores.txt");
			cout << "You have selected '2.' Here are the current highscores" << endl;
			writefile >> x;
			cout << x << endl;

		}
		else if (option == 3)
		{
			cout << "Exiting program" << endl;
			break;
		}
	}
	

	system("pause");
	return 0;
}
Use a loop, reading scores until none are left.
You should also prefer to keep fstream objects in as small a namespace as possible.

1
2
3
4
5
6
7
8
  else if (option == 2)
  {
    ifstream scoresfile( scores_file_name );
    cout << "You have selected '2.' Here are the current highscores\n";
    int x;
    while (scoresfile >> x)
      cout << x << "\n";
  }

Typically a high-scores file will also list the name of the player who scored so well. You can do that easily enough: put the number first and the name last. Your highscores.txt file might look something like this:
197 John the Bomb
188 Amy Kicked your A*
72 Timmy

Write the file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

struct score
{
  string player;
  int score;
};

constexpr string HIGHSCORES_TXT = "highscores.txt";

void write_highscores( vector <score> scores, string filename = HIGHSCORES_TXT )
{
  ofstream f( filename );
  for (auto score : scores)
    f << score.score << " " << score.player << "\n";
}

vector <score> read_highscores( string filename = HIGHSCORES_TXT )
{
  vector <score> scores;
  ifstream f( filename );
  score s;
  while (f >> s.score >> ws && getline( f, score.player ))
    scores.push_back( score );
  return scores;
}

int main()
{
  vector <score> highscores = read_highscores();

  cout << "The high scores are:\n";
  for (auto score : highscores)
    cout << left << setw( 30 ) << score.player << " " << score.score << "\n";

  // Add a new high score
  scores.push_back( score { 200, "Sniper" } );
  sort( scores.begin(), scores.end(), []( score a, score b ) { return a.score > b.score; } );

  write_highscores( scores );
}

Hope this helps.
So i can write 10 numbers to a file, and when i open the file all 10 numbers are there. But when i want to read from the file, it only reads the last number. How do i change it so it reads everything from the file. Is there a way i can just read the whole file and output it?
Is there a way i can just read the whole file and output it?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>

using namespace std;

// So i can write 10 numbers to a file, and when i open the file
// all 10 numbers are there.

void create_file(const char * fname)
{
    ofstream fout(fname);

    for (int n : { 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 })
        fout << n << '\n';
}

int main()
{
    const char * filename = "data.txt";

    // first create a file for testing

    create_file(filename);

    // Just read the whole file and output it.

    ifstream fin( filename );

    int m;
    while ( fin >> m )
        cout << m << '\t';
}
Last edited on
Registered users can post here. Sign in or register to post.