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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:05:19+00:00 2026-06-03T00:05:19+00:00

I have an assignment where I am writing input on various things (in the

  • 0

I have an assignment where I am writing input on various things (in the form of structs) and then writing to a binary file. I have to be able to both read and write to the file while the program is open. One of the methods needs to print out all of the clients in the binary file. It seems to be working, except whenever I call that method, it seems to erase the contents of the file and prevent more from being written to it. Here’s the applicable snippets:

fstream binaryFile;
binaryFile.open("HomeBuyer", ios::in | ios::app | ios::binary);

The same file is supposed to be usable between times you run the program, so I should open it with ios::app, correct?

Here’s the method to add an entry:

void addClient(fstream &binaryFile) {
      HomeBuyer newClient; //Struct the data is stored in
      // -- Snip -- Just some input statements to get the client details //

      binaryFile.seekp(0L, ios::end); //This should sent the write position to the
                                     //end of the file, correct?
      binaryFile.write(reinterpret_cast<char *>(&newClient), sizeof(newClient));

      cout << "The records have been saved." << endl << endl;
}

And now the method to print all the entries:

void displayAllClients(fstream &binaryFile) {
    HomeBuyer printAll;
    binaryFile.seekg(0L, ios::beg);
    binaryFile.read(reinterpret_cast<char *>(&printAll),sizeof(printAll));

    while(!binaryFile.eof()) {  //Print all the entries while not at end of file
        if(!printAll.deleted)  {
             // -- Snip -- Just some code to output, this works fine //
        }

        //Read the next entry
        binaryFile.read(reinterpret_cast<char *>(&printAll),sizeof(printAll)); 
    }
    cout << "That's all of them!" << endl << endl;
}

If I step through the program, I can input as many clients as I want, and it will output them all the first time I call displayAllClients(). But as soon as I call displayAllClients() once, it seems to clear out the binary file, and any further attempts at displaying clients gives me no results.

Am I using seekp and seekg incorrectly?

From what I understand, this should set my write position to the end of the file:

binaryFile.seekp(0L, ios::end);

And this should set my read position to the beginning:

binaryFile.seekg(0L, ios::beg);

Thanks!

  • 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-06-03T00:05:21+00:00Added an answer on June 3, 2026 at 12:05 am

    Pasting comment in as this resolved the issue.

    You need to call binaryFile.clear() before seekp() and seekg() if EOF is set, otherwise they won’t work.

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

Sidebar

Related Questions

I have an assignment about fftw and I was trying to write a small
I have an assignment that wants me to write an ternary search algorithm and
For a university assignment, we have to modify the ext2 file system to store
I have a question about writing your own init methods in objective-c. I've read
I'm writing a little thing for an assignment, and I have to manage TCP
I have an assignment that has asked me to copy a file using buffered
I have assignment to work on producer and consumer problem by use thread and
Is it possible to have assignment in a condition? For ex. if (a=some_func()): #
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have an assignment to create a secure communication between 2 people with a

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.