Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 803301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:44:16+00:00 2026-05-14T23:44:16+00:00

I am writing a high-score sub-routine for a text-based game. Here’s what I have

  • 0

I am writing a high-score sub-routine for a text-based game. Here’s what I have so far.

void Game::loadHiScores(string filename)
{
    fstream hiscores(filename.c_str()); // what flags are needed?
    int score;
    string name;
    Score temp;

    if (hiscores.is_open())
    {
        for (size_t i = 0; i < TOTAL_HISCORES && !(hiscores.eof()); ++i)
        {
            cin >> score >> name;
            temp.addPoints(score);
            temp.scoreSetName(name);
            hiScores.push_back(temp);
        }
    }
    else
    {
        //what should go here?
    }   

    hiscores.close();

}

How can I make it so that:

IF the file exists, it shall be open for reading

ELSE the file shall be created

Thanks for your time

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T23:44:16+00:00Added an answer on May 14, 2026 at 11:44 pm

    Wrong logic I would say. I think you want:

    Try to open an ifstream (not an fstream) containing scores
    if it opened
       read high scores into array
       close stream
    else
       set array to zero
    endif
    
    Play Game - adjust high scores in array, then on exit
    
    Try to open scores ofstream (not an fstream) for writing
    if it opened
        write high scores
        close stream
    else
        report an error
    end if
    

    If you use ifstreams and ofstreams, there is no need for special flags, or for possibly having to seek within the file – you just rewrite the whole thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's a real noobish MySQL query problem I'm having. I have a high score
my cocos2d game high score's are written into a plist. Every time i go
I'm writing a small online flash game that keeps high scores of players. Obviously
I'm writing a high load application that uses SQL Server Service Broker. I have
I'm writing a high-loaded daemon that should be run on the FreeBSD 8.0 and
I am writing a High Performance ASP.NET Json API with soon > 1000 Request/Second.
I'm writing a program that needs to print off high resolution .tiff images. The
I am writing a small program for our local high school (pro bono). The
I am writing a dll with a number of high-level utility classes, that other
Writing a test app to emulate PIO lines, I have a very simple Python/Tk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.