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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:43:06+00:00 2026-05-14T03:43:06+00:00

when I run this code, the open and seekg and tellg operation all success.

  • 0

when I run this code, the open and seekg and tellg operation all success.
but when I read it, it fails, the eof,bad,fail bit are 0 1 1.

What can cause a file bad?
thanks


int readriblock(int blockid, char* buffer)
{
   ifstream rifile("./ri/reverseindex.bin", ios::in|ios::binary);

   rifile.seekg(blockid * RI_BLOCK_SIZE, ios::beg);
   if(!rifile.good()){ cout<<"block not exsit"<<endl; return -1;}
   cout<<rifile.tellg()<<endl;

   rifile.read(buffer, RI_BLOCK_SIZE);

   **cout<<rifile.eof()<<rifile.bad()<<rifile.fail()<<endl;**

   if(!rifile.good()){ cout<<"error reading block "<<blockid<<endl; return -1;}

   rifile.close();
   return 0;
}

  • 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-14T03:43:07+00:00Added an answer on May 14, 2026 at 3:43 am

    Quoting the Apache C++ Standard Library User’s Guide:

    The flag std::ios_base::badbit indicates problems with the underlying stream buffer. These problems could be:

    • Memory shortage. There is no memory available to create the buffer, or the buffer has size 0 for other reasons (such as being provided from outside the stream), or the stream cannot allocate memory for its own internal data, as with std::ios_base::iword() and std::ios_base::pword().
    • The underlying stream buffer throws an exception. The stream buffer might lose its integrity, as in memory shortage, or code conversion failure, or an unrecoverable read error from the external device. The stream buffer can indicate this loss of integrity by throwing an exception, which is caught by the stream and results in setting the badbit in the stream’s state.

    That doesn’t tell you what the problem is, but it might give you a place to start.

    Keep in mind the EOF bit is generally not set until a read is attempted and fails. (In other words, checking rifile.good after calling seekg may not accomplish anything.)

    As Andrey suggested, checking errno (or using an OS-specific API) might let you get at the underlying problem. This answer has example code for doing that.

    Side note: Because rifile is a local object, you don’t need to close it once you’re finished. Understanding that is important for understanding RAII, a key technique in C++.

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

Sidebar

Related Questions

I am ready to run this code but before I want to fix the
Why this code don't work,when i want run this code vwd 2008 express show
When I run this code in iPhone simulator ,I can not delete record from
Hi I'm trying to load raw machine code into memory and run it from
I tried to run the following code from http://docs.python.org/library/mmap.html import mmap # write a
I wish to open a binary file, to read the first byte of the
Is it possible to open a file in .NET with non exclusive write access?
I'm trying to serialize/deserialize string. Using the code: private byte[] StrToBytes(string str) { BinaryFormatter
Currently I have some code to replace strings in a file that looks like
$file_name = 'New Folder.zip' $zip = new ZipArchive; $result = $zip->open($target_path.$file_name); if ($result ===

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.