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

The Archive Base Latest Questions

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

Im having a small problem with my code. I have been trying to read

  • 0

Im having a small problem with my code. I have been trying to read some information from the end of a random access file then repositioning the cursor at the beginning of the file. So far seekg has not been working. Here is my code:

void main ()
{
    AssignData FileOut;
    AssignData FileIn;

    ofstream OutData("Data.dat", ios::out | ios::app | ios::binary);
    ifstream InData("Data.dat", ios::in);

    cout <<"Writing to file" << endl;

    if (!OutData)
    {
         cout<<"File does not exist"<<endl;
     exit(1);
    }

    //read through file

    while (InData.read( reinterpret_cast< char * >( &FileIn ),sizeof( AssignData ) ))
    {                        
    }

    FileIn.DisplayFileContent();//displays the last line in the file

    FileOut.setInfo();//allows user to add another record

    //Writes record to file
    OutData.write( reinterpret_cast< const char * >( &FileOut ), sizeof(AssignData) );                                        

    OutData.close();
    cout << endl<<endl;
    //Reads the first record from the file

    InData.seekg(0);

    while (InData.read( reinterpret_cast< char * >( &FileIn), sizeof(AssignData) ))
    {
    // display record
        if ( (FileIn.getID() != 0) || (FileIn.getID()!=NULL) )
    {
        FileIn.DisplayFileContent();
    }                         
    }

    InData.close();

    _getch();
    exit(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-31T21:43:16+00:00Added an answer on May 31, 2026 at 9:43 pm

    To go to the end of the file:

    ifstream is;
    is.open( "file.txt", ios::binary );
    is.seekg (0, ios::end);
    

    To get the length of the file (when at the end), use tellg which returns the absolute position of the get pointer.

    length = is.tellg();
    

    To go back to the beginning use seekg again, this sets the position of the put pointer.

    is.seekg (0, ios::beg);
    

    If you have tried this could you be more specific about what isn’t working?

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

Sidebar

Related Questions

I have been having a small problem that I can't by-pass it, I keep
I'm having a small problem in Java. I have an interface called Modifiable. Objects
I'm having a small problem. I have this grid with a column: <data:DataGrid ColumnHeaderStyle={StaticResource
We're having a small issue and could use some help - we have the
having a small problem. I'm trying to create a loop that will create an
I'm having a small problem, that I just cannot figure out. I have a
I am having a small problem geting to some data within my MVC C#
I'm having a small problem with ASP.NET MVC and Entity Framework 4. I have
I'm having a small but annoying problem with trying to open a link in
Im having a small problem with storage of special characters like quotes, double quotes

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.