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

  • Home
  • SEARCH
  • 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 8336945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:08:51+00:00 2026-06-09T04:08:51+00:00

I am trying to enter data through classes in a binary file using file

  • 0

I am trying to enter data through classes in a binary file using file handling. But what I am unable to do is to write data in a sorted manner. Like I can’t figure out how can I uniquely identify one of the records I had inserted in the past in that particular file. Why I am trying to do so? Because I want to be able to perform operations like deleting, modifying a particular record. I tried using static variable but I want to hold that value until I run the program the next time which is not possible with static. BTW this is the class.

class question
{
    private:
    char question[500];
    char option1[25],option2[25],option3[25],option4[25];
    int answernumber;
    public:
    int qno;
    void createquestion(); // Just to to accept data into
                           // question,options,answernumebr.
}

Now the only problem is assigning qno which is question number. I am planning to use it as my Primary key( wrong to use this word) . But I just can’t figure it out how to automatically assign it its corresponding values without user intervention? Any suggestions?
EDIT:- I also tried to do this—> I thought of reading the last record entered and then extract the corresponding question number. And then assign that number and add 1 to it…so old_qno+1=new_qno….. But unfortunately didn’t worked out too well. as All my questions were being assigned as question 1 only

  • 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-09T04:08:52+00:00Added an answer on June 9, 2026 at 4:08 am

    You can use the size of the file you have written as way of counting of the number questions you have so far.

    ifstream file("filename");
    file.seekg(0, ios_base::end);
    streampos file_size = file.tellg();
    file.close();
    int next_question = file_size/sizeof(question) + 1;
    

    But you have to be careful with deletes, suppose you have questions 1, 2, 3, 4 in your file and then you delete question 3. Now your file has 1, 2, 4 and so the method above will say the next question is 4, but you already have a question 4. One way to deal with this is to do logical deletes. Instead of actually deleting a question from a file you leave it there but mark it in some way so you know it’s deleted. For instance you could set the question number to zero. So going back to the example above after you deleted question three you would have 1, 2, 0, 4. Now the file size method will correctly say the next question is 5.

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

Sidebar

Related Questions

I'm trying to allow my users to enter in large lists of data using
I'm trying to require the user to enter data in at least one of
I am trying to create a list where I want users to enter data
I'm trying to create a procedure that will enter data and then return a
I'm trying to create a child form that helps the user to enter data
I'm trying to enter edit mode on a specific cell like this: void MainWindow::on_addButton_released()
i'm trying to get the portletPreferences enter in my ConfigActionImpl which is like this
I am trying to ignore the enter key, but i do not want to
Working through C++ Primer Plus and am trying to cin data to a dynamically
Hey guys I'm trying to work my way through this but am having an

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.