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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:15+00:00 2026-05-26T23:20:15+00:00

Suppose I have a struct and a file with binary representations of those structs

  • 0

Suppose I have a struct and a file with binary representations of those structs and I’ll make a function/method that access this binary data using ifstream::read().

Here’s an example struct:

struct MyStruct {
    int x; //Value interested in
    int y; //Value interested in
    int anotherInteger; //Not interested
    double aDouble; //Not interested
}

How do I make the function (I’ll call it here readData) either: not using pointers when reading or, if using pointers is necessary, where would I put the proper delete?

So far, the relevant part of my readData looks like this:

void readData(int position, int &returnX, int &returnY) {
    ifstream inFile("binaryFile.dat",ios::binary);

    MyStruct *st = new MyStruct[1];

    inFile.seekg(sizeof(MyStruct)*pos);
    inFile.read((char*) st, sizeof(MyStruct));

    returnX = st[0].x;
    returnY = st[0].y;

    //delete [] st goes here?
}

I’ve tried uncommenting the delete part, but I get an allocation error, probably because the values of x and y are pointing to something that doesn’t exist anymore.

Any ideas on how to solve this?

  • 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-26T23:20:15+00:00Added an answer on May 26, 2026 at 11:20 pm

    Why wouldn’t you use a local variable?

    void readData(int position, int &returnX, int &returnY) {
        ifstream inFile("binaryFile.dat",ios::binary);
        inFile.seekg(sizeof(MyStruct)*position);
    
        MyStruct st;    
        inFile.read((char*) &st, sizeof(MyStruct));
    
        returnX = st.x;
        returnY = st.y;
    }
    
    int main() {
        int mainx, mainy;
        readData(0, mainx, mainy);
        return 0;
    }
    

    Also, references cannot be re-seated. Therefore the assignment assigns the value to the origional int passed by the calling function. returnX and returnY are not pointed at the local variables. In the code above, the assignment changes mainx and mainy.

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

Sidebar

Related Questions

Suppose we have a binary file, that contains 32 bit numbers. Each 32bit number
Suppose I have a stringbuilder in C# that does this: StringBuilder sb = new
I have used this tutorial to make a website, but I would like that
Suppose I compile a source file file which contains this piece of code, struct
I have a struct that I'd like to export to a file, and then
Suppose I have a C# struct like this: [StructLayout(LayoutKind.Explicit)] struct IMAGE_DOS_HEADER { [FieldOffset(60)] public
I have a structure that only one function must access. The function converts tokens
Suppose I have this struct (which incidentally contain bit-fields, but you shouldn't care): struct
Suppose I have this struct: struct vector_data { double x, y; double& operator[](size_t index)
Suppose I have this code: struct Normal { public float x; public float y;

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.