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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:25:47+00:00 2026-06-15T12:25:47+00:00

Why does this file always contain an extra Fraction structure? If a user enters

  • 0

Why does this file always contain an extra Fraction structure? If a user enters option 3 to view all fractions when the program just starts (the file should be empty), 0/0 is printed out. If a user selects option one and enters a fraction 1/1, and then selects option 3, two fractions are printed out 1/1 1/1.

Note: to run this you will need to create an empty .dat file called "fractions.dat"

I really appreciate your help!

#include <iostream>
#include <fstream>

using namespace std;

struct Fraction
{
    int num, den;
};

int search(fstream& file, Fraction* f);
int menu();
void proccessChoice(int i);
Fraction* readFrac();

Fraction* fracs[100];
int index;
string fileName = "fractions.dat";
fstream file(fileName,  ios::in | ios::out | ios::binary | ios::trunc);

//structure called Fraction with num and den as its only integer members
int main()
{
    if (!file)
    {
        cout << "Error opening file. Program aborting.\n";
        system("pause");
        exit(1);
    }
    int choice;
    do
    {
        choice = menu();
        proccessChoice(choice);
    } while(choice != 4);


    system("pause");
    return 0;
}



int menu()
{
    int c;
    cout << "1.Enter new fraction" << endl;
    cout << "2.Display/update a fraction" << endl;
    cout << "3.Display all fractions" << endl;
    cout << "4.Quit" << endl;
    cin >> c;
    return c;
}

void proccessChoice(int i)
{
    switch(i)
    {
    case 1:
        {
            cout << "Enter a fraction to add: ";
            fracs[index] = readFrac();
            file.seekp(0, ios::end);
            file.write(reinterpret_cast<char*>(fracs[index]), sizeof(Fraction));
            /*cout << fracs[index]->num << "/" << fracs[index]->den ;*/
            index++;
        }
            break;
    case 2:
        {
            cout << "Enter fraction to view/update: ";
            Fraction* fToFind = readFrac();
            int i = search(file, fToFind);
            if (i == -1)
            {
                cout << "Fraction not found." << endl;
            }
            else
            {
                char r;
                cout << fToFind->num << "/" << fToFind->den << " found at index " << i << ". Update? [y/n]";
                cin >> r;
                if(r == 'y' || r == 'Y')
                {
                    cout << "Enter a new fraction: ";
                    Fraction* fToWrite = readFrac();
                    file.seekp(sizeof(Fraction) * (i));
                    file.write(reinterpret_cast<char*>(fToWrite), sizeof(Fraction));
                }
            }
            cout << i << endl;
            cout << "Case 2" << endl;
        }
            break;
    case 3:
        {
            Fraction* cur = new Fraction();
            //int pos = -1;
            if (!file)
            {
                cout << "Error opening file. Program aborting.\n";
                system("pause");
                exit(1);
            }
            file.seekg(0, ios::beg);
            while(!file.eof())
            {
                file.read(reinterpret_cast<char *>(cur), sizeof(Fraction));
                cout << cur->num << "/" << cur->den << endl;
            }
                    cout << "Case 3" << endl;
        }
            break;
    }
}

int search(fstream& file, Fraction* f)
{
    Fraction* cur = new Fraction();
    int pos = -1;
    if (!file)
    {
        cout << "Error opening file. Program aborting.\n";
        system("pause");
        exit(1);
    }
    file.seekg(0, ios::beg);
    while(!file.eof())
    {
        file.read(reinterpret_cast<char *>(cur), sizeof(Fraction));
        if((cur->num == f->num) && (cur->den == f->den))
        {
            cout << "\nFrac found!" << cur->num << "/" << cur->den<<endl;
            pos = file.tellg()/sizeof(Fraction) - 1;
            return pos;
        }
    }
    return pos;
}

Fraction* readFrac()
{
    Fraction* f = new Fraction();
    char slash;
    cin >> f->num >> slash >> f->den;
    return f;
}

Attempted solution 1: works for the first attempt of the user to view all fractions but on the second attempt the file is not found.

case 3:
        {
            Fraction* cur = new Fraction();
            //int pos = -1;
            if (!file)
            {
                cout << "Error opening file. Program aborting.\n";
                system("pause");
                exit(1);
            }
            file.seekg(0, ios::beg);
            while(!file.eof())
            {
                if(file.read(reinterpret_cast<char *>(cur), sizeof(Fraction)).gcount()== sizeof(Fraction))
                {
                    cout << cur->num << "/" << cur->den << endl;
                }
            }
                    cout << "Case 3" << endl;
        }
            break;
    }
  • 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-15T12:25:49+00:00Added an answer on June 15, 2026 at 12:25 pm

    Because you didn’t yet reach the end of the file!

    while(!file.eof())
    

    You opened the empty file, however, note that the read cursor is currently at position 0. As long as you didn’t attempt to read data file.eof() will be false, as the end of file hasn’t been reached yet by the cursor.

    You’ll need to check whether you were actually able to extract the Fraction:

    if(
       file.read(reinterpret_cast<char *>(cur), sizeof(Fraction)).gcount() 
       == sizeof(Fraction)
    ){
         // Fraction has been extracted
    }
    else
         // either eof() or some other error.
    

    Note that this gets much simpler if you provide a custom operator for this:

    std::istream& operator>>(std:istream& in, Fraction& f){
        return in.read(reinterpret_cast<char *>(&f), sizeof(Fraction));
    }
    
    // ....
    while(file >> *cur){
       // ... your code.
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why does this graphml file below does not render the node color properly in
Why does this output a 0 byte file? <?php $jsonurl = http://do.convertapi.com/Web2Pdf/json/?curl=http://stackoverflow.com/; $json =
Does anyone know where I could find this file on Ubuntu?
This works: require 'csv' file = CSV.open(filename) puts file.shift This does not: require 'csv'
In a .f file there is code that does this: real Bob, avar ...
For some reason, this code does not actually draw my bitmap file... or show
Why does Java not have a file copy method? This seems like such an
Does anyone see a problem with this, its not working saying bad file descriptor
What does VAR_NAME=${VAR_NAME:-/some/path/file} mean in an shell script? This is for an init script,
I'm processing a file with PHP. This file contains a few blocks, which always

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.