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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T21:00:31+00:00 2026-05-10T21:00:31+00:00

I am implementing a very simple file database. I have 2 basic operations: void

  • 0

I am implementing a very simple file database. I have 2 basic operations:

void Insert(const std::string & i_record) {    //create or append to the file     m_fileStream.open(m_fileName.c_str(), std::ios::out | std::ios::app);      if (m_fileStream.is_open())     {         m_fileStream << i_record << '\n';     }      m_fileStream.flush();     m_fileStream.close(); }  /* * Returns a list with all the items in the file. */  std::vector<std::string> SelectAll()  {     std::vector<std::string> results;      m_fileStream.open(m_fileName.c_str(), std::ios::in);      std::string line;     if (m_fileStream.is_open())     {         while (!m_fileStream.eof())         {             getline (m_fileStream, line);             results.push_back(line);          }     }      m_fileStream.close();      return results;  }     

the class has m_fileStream and m_fileName as private members.

OK – here’s the problem:

If I do something like:

db->Insert('a'); db->SelectAll(); db->Insert('b'); 

The end result is that the file will contain only ‘a’; WHY?

NOTE: it seems that getline() will set the fail bit. but why?

  • 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. 2026-05-10T21:00:31+00:00Added an answer on May 10, 2026 at 9:00 pm

    Change

        while (!m_fileStream.eof())     {         getline (m_fileStream, line);         results.push_back(line);      } 

    to

        while (getline (m_fileStream, line))     {         results.push_back(line);     } 

    Otherwise you will get one additional empty line at the end. eof() will return true only once you tried to read past the end of the file, and not if only the next read would be past the end of the file.

    It sets the failbit because getline tries to extract characters from the stream. If there are no characters left (and no '\n' has been seen yet), stream.get(c) to a character will set the failbit. Then getline will set the eofbit and then .eof() will return true, and your loop exits.

    If you don’t want failbit set, then change your condition from !stream.eof() to stream.peek() != EOF (and make sure there is a trailing newline in your file).

    This now is also the solution to your problem: .close() doesn’t .clear() your stream, so the failbit still is set if you reopen your file. call stream.clear() after reading your stuff in, and then it works.

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

Sidebar

Ask A Question

Stats

  • Questions 69k
  • Answers 69k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer MySQLPerformanceBlog.com did some benchmarks in an article about 'Prepared Statements.'… May 11, 2026 at 12:31 pm
  • added an answer It's possible for a file to have no extension -… May 11, 2026 at 12:31 pm
  • added an answer You can configure a linked server from SQL Server to… May 11, 2026 at 12:31 pm

Related Questions

I am implementing a HttpRequestValidationException in my Application_Error Handler, and if possible, I want
I am implementing a quite simple state-machine order processing application. It is a e-commerce
I am implementing a comment control that allows a person to select comments and
I am implementing a design that uses custom styled submit-buttons. They are quite simply
What is the most preferred format of unicode strings in memory when they are
In the application, when special types of objects are created, I need to generate
Does anyone know whether a DOM Node of type Text is guaranteed not be
I am implementing a tree think of it as a folder structure so I

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.