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 4231002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T01:55:23+00:00 2026-05-21T01:55:23+00:00

If a program tries to use my file-reading class to access part of a

  • 0

If a program tries to use my file-reading class to access part of a file that does not exist (i.e., is after ios_base::end), what exception should be thrown?

I was going to use std::out_of_range, but now I’m beginning to think std::runtime_error would be more appropriate.

Edit:

The interface works a bit differently than the standard fstream class in that it’s based around for loops rather than while loops. So instead of doing the following:

while (MyFile.peek() != EOF)
{
    char Character = MyFile.get();
    // Do stuff with Character.
}

The user of the class can do this:

for (std::streampos Pos = 0; Pos < MyFile.GetCharCount(); Pos++)
{
    char Character = MyFile.GetChar(Pos);
    // Do stuff with Character.
}
  • 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-21T01:55:24+00:00Added an answer on May 21, 2026 at 1:55 am

    If you’re providing an array-like interface (using operator[], rather
    than get), then throwing std::out_of_range is a good idea; a better
    idea in this case might be to figure out how many bytes you can actually
    access, furnish a size() function, and make being in range
    a pre-condition. (Of course, to provide random access like this, you’re
    going to have to read the entire file into memory anyway. You can’t
    seek to arbitrary positions at random in a text file, except under
    Unix.)

    If you’re providing a file like interface, then setting an error status,
    like iostream does, is the preferred solution.

    If you’re providing neither, it’s all a question of what you are
    providing, and what user expectations for it are, or should be.

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

Sidebar

Related Questions

When I try opening a file it tries to use C:\Program Files\Common Files\Microsoft Shared\DevServer\9.0
I'm encountering a hang when the program tries to access the fruit database. I've
I have a program that tries to shrink a double down to a desired
Here is a C# program that tries Marshal.SizeOf on a few different types: using
I have written an ActionScript client program that tries to connect to a local
I'm trying to use SCons to compile a program that requires a set of
I'm reading in from a .txt file that looks something along the lines of:
Is there any apt-get -like program for use with Cygwin? I've already tried cyg-apt
In my program, if a user tries to leave a page, he'll receive a
Short summary : My Qt class crashes at class construction part when calling the

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.