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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:03:11+00:00 2026-06-18T06:03:11+00:00

I’m writing a resource file which I want to insert a bunch of data

  • 0

I’m writing a resource file which I want to insert a bunch of data from various common files such as .JPG, .BMP (for example) and I want it to be in binary.
I’m going to code something to retrieve these data later on organized by index, and this is what I got so far:

float randomValue = 23.14f;

ofstream fileWriter;
fileWriter.open("myFile.dat", ios::binary);
fileWriter.write((char*)&randomValue, sizeof(randomValue));
fileWriter.close();
//With this my .dat file, when opened in notepad has "B!¹A" in it

float retrieveValue = 0.0f;

ifstream fileReader;
fileReader.open("myFile.dat", ios::binary);
fileReader.read((char*)&retrieveValue, sizeof(retrieveValue));
fileReader.close();

cout << retrieveValue << endl; //This gives me exactly the 23.14 I wanted, perfect!

While this works nicely, I’d like to understand what exactly is happening there.
I’m converting the address of randomValue to char*, and writing the values in this address to the file?

I’m curious also because I need to do this for an array, and I can’t do this:

int* myArray = new int[10];
//fill myArray values with random stuff

fileWriter.open("myFile.dat", ios::binary);
fileWriter.write((char*)&myArray, sizeof(myArray));
fileWriter.close();

From what I understand, this would just write the first address’ value in the file, not all the array. So, for testing, I’m trying to simply convert a variable to a char* which I would write to a file, and convert back to the variable to see if I’m retrieving the values correctly, so I’m with this:

int* intArray = new int[10];
for(int i = 0; i < 10; i++)
{
    cout << &intArray[i]; //the address of each number in my array
    cout << intArray[i]; //it's value
    cout << reinterpret_cast<char*>(&intArray[i]); //the char* value of each one     
}

But for some reason I don’t know, my computer “beeps” when I run this code. During the array, I’m also saving these to a char* and trying to convert back to int, but I’m not getting the results expected, I’m getting some really long values.
Something like:

float randomValue = 23.14f;

char* charValue = reinterpret_cast<char*>(&randomValue);
//charValue contains "B!¹A" plus a bunch of other (un-initiallized values?) characters, so I'm guessing the value is correct

//Now I'm here

I want to convert charValue back to randomValue, how can I do it?

edit: There’s valuable information in the answers below, but they don’t solve my (original) problem. I was testing these type of conversions because I’m doing a code that I will pick a bunch of resource files such as BMP, JPG, MP3, and save them in a single .DAT file organized by some criteria I still haven’t fully figured out.

Later, I am going to use this resource file to read from and load these contents into a program (game) I’m coding.

The criteria I am still thinking but I was wondering if it’s possible to do something like this:

//In my ResourceFile.DAT
[4 bytes = objectID][3 bytes = objectType (WAV, MP3, JPG, BMP, etc)][4 bytes = objectLength][objectLength bytes = actual objectData]
//repeating this until end of file

And then in the code that reads the resource file, I want to do something like this (untested):

ifstream fileReader;
fileReader.open("myFile.DAT", ios::binary);
//file check stuff
while(!fileReader.eof())
{
    //Here I'll load
    int objectID = 0;
    fileReader((char*)&objectID, 4); //read 4 bytes to fill objectID

    char objectType[3];
    fileReader(&objectType, 3); //read the type so I know which parser use

    int objectLength = 0;
    fileReader((char*)&objectLength, 4); //get the length of the object data

    char* objectData = new char[objectLength];
    fileReader(objectData, objectLength); //fill objectData with the data

    //Here I'll use a parser to fill classes depending on the type etc, and move on to the next obj
}

Currently my code is working with the original files (BMP, WAV, etc) and filling them into classes, and I want to know how I can save the data from these files into a binary data file.
For example, my class that manages BMP data has this:

class FileBMP
{
    public:
        int imageWidth;
        int imageHeight;
        int* imageData;
}

When I load it, I call:

void FileBMP::Load(int iwidth, int iheight)
{
    int imageTotalSize = iwidth * iheight * 4;
    imageData = new int[imageTotalSize]; //This will give me 4 times the amount of pixels in the image

    int cPixel = 0;
    while(cPixel < imageTotalSize)
    {
        imageData[cPixel] = 0;     //R value
        imageData[cPixel + 1] = 0; //G value
        imageData[cPixel + 2] = 0; //B value
        imageData[cPixel + 3] = 0; //A value
        cPixel += 4;
    }
} 

So I have this single dimension array containing values in the format of [RGBA] per pixel, which I am using later on for drawing on screen.
I want to be able to save just this array in the binary data format that I am planning that I stated above, and then read it and fill this array.

I think it’s asking too much for a code like this, so I’d like to understand what I need to know to save these values into a binary file and then read back to fill it.

Sorry for the long post!

edit2: I solved my problem by making the first edit… thanks for the valuable info, I also got to know what I wanted to!

  • 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-18T06:03:12+00:00Added an answer on June 18, 2026 at 6:03 am

    By using the & operator, you’re getting a pointer to the contents of the variable (think of it as just a memory address).

    float a = 123.45f;
    float* p = &a; // now p points to a, i.e. has the memory address to a's contents.
    char* c = (char*)&a; // c points to the same memory location, but the code says to treat the contents as char instead of float.
    

    When you gave the (char*)&randomValue for write(), you simply told “take this memory address having char data and write sizeof(randomValue) chars from there”. You’re not writing the address value itself, but the contents from that location of memory (“raw binary data”).

    cout << reinterpret_cast<char*>(&intArray[i]); //the char* value of each one 
    

    Here you’re expected to give char* type data, terminated with a null char (zero). However, you’re providing the raw bytes of the float value instead. Your program might crash here, as cout will input chars until it finds the terminator char — which it might not find anytime soon.

    float randomValue = 23.14f;
    char* charValue = reinterpret_cast<char*>(&randomValue);
    
    float back = *(float*)charValue;
    

    Edit: to save binary data, you simply need to provide the data and write() it. Do not use << operator overloads with ofstream/cout. For example:

        int values[3] = { 5, 6, 7 };
    struct AnyData
    {
       float a;
       int b;
    } data;
    
    cout.write((char*)&values, sizeof(int) * 3); // the other two values follow the first one, you can write them all at once.
    cout.write((char*)&data, sizeof(data)); // you can also save structs that do not have pointers.
    

    In case you’re going to write structs, have a look at #pragma pack compiler directive. Compilers will align (use padding) variable to certain size (int), which means that the following struct actually might require 8 bytes:

    #pragma pack (push, 1)
    struct CouldBeLongerThanYouThink
    {
      char a;
      char b;
    };
    #pragma pack (pop)
    

    Also, do not write pointer values itself (if there are pointer members in a struct), because the memory addresses will not point to any meaningful data once read back from a file. Always write the data itself, not pointer values.

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

Sidebar

Related Questions

I have a bunch of posts stored in text files formatted in yaml/textile (from
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I want to construct a data frame in an Rcpp function, but when I
I'm parsing an XML file, the creators of it stuck in a bunch social
i want to parse a xhtml file and display in UITableView. what is the
I am writing an app for my school newspaper, which is run completely online
I am using jsonparser to parse data and images obtained from json response. When
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.