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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:52:23+00:00 2026-05-23T16:52:23+00:00

For a while now, I have been experiencing an extremely odd problem when trying

  • 0

For a while now, I have been experiencing an extremely odd problem when trying to write memory to a filebuffer in C++. The problem only occurs on MinGW. When I compile under gcc/linux, everything is fine.

Debugging Session displaying the problem

So basically, I’m writing code from a memory buffer to a filebuffer, and the binary representation in the file ends up being different from the memory I wrote. No, the file is not being modified at a later point, I ensured this by using the debugger to exit the program after closing the file. I have no idea how something like this is even possible, I even used valgrind to see if there were any memory allocation problems, but nope.

I’ll paste some of the related code.

/// a struct holding information about a data file
class ResourceFile {
public:
    string name;
    uint32 size;
    char* data;

    ResourceFile(string name, uint32 size);
};

ResourceFile::ResourceFile(string name, uint32 size)
    : name(name), size(size)
{
    // will be free'd in ResourceBuilder's destruction
    data = (char*) malloc(size * sizeof(char));
}


/// Build a data resource from a set of files
class ResourceBuilder {
public:
    ofstream out; ///< File to put the resource into
    vector<ResourceFile> input; ///< List of input strings

    /// Add a file from disk to the resource
    void add_file(string filename);

    /// Create a file that the resource will be written to
    void create_file(string filename);

    ~ResourceBuilder();
};

void ResourceBuilder::create_file(string filename) {
    // open the specified file for output
    out.open(filename.c_str());
    uint16 number_files = htons(input.size());
    out.write((char*) &number_files, sizeof(uint16));

    foreach(vector<ResourceFile>,input,i) {
        ResourceFile& df = *i;
        uint16 name_size = i->name.size();
        uint16 name_size_network = htons(name_size);
        out.write((char*) &name_size_network, sizeof(uint16));
        out.write(i->name.c_str(),name_size);
        uint32 size_network = htonl(i->size);
        out.write((char*) &size_network, sizeof(i->size) );
        out.write(i->data, i->size);
    }

    out.close();

    /// \todo write the CRC
}

The following is how the memory is allocated in the first place. This is a possible source of error, because I copypasted it from somewhere else without bothering to understand it in detail, but I honestly don’t know how the method in which I allocated memory could be a reason for filebuffer output being different from the memory that I’m writing.

void ResourceBuilder::add_file(string filename) {
    // loads a file and copies its content into memory
    // this is done by the ResourceFile class and there is a
    // small problem with this, namely that the memory is
    // allocated in the ResourceFile directly,
    ifstream file;
    file.open(filename.c_str());

    filebuf* pbuf=file.rdbuf();

    int size=pbuf->pubseekoff (0,ios::end,ios::in);
    pbuf->pubseekpos (0,ios::in);

    ResourceFile df(filename,size);

    pbuf->sgetn (df.data,size);

    file.close();

    input.push_back(df);
}

I’m really out of ideas. It’s also not a bug pertaining to my compiler setup, as other people compiling the code under MinGW get the same error. The only explanation I can think of at this point is a bug with MinGW’s filebuffer library itself, but I honestly have no idea.

  • 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-23T16:52:23+00:00Added an answer on May 23, 2026 at 4:52 pm

    You need to open the file in binary mode. When you open it in text mode on Windows, line feeds (0x0A) will get converted to CR/LF pairs (0x0D, 0x0A). On Linux you don’t see this because Linux uses a single LF (0x0A) as the line terminator in text files, so no conversion is done.

    Pass the ios::binary flag to the ostream::open method:

    out.open(filename.c_str(), ios::out | ios::binary);
    

    You should also use this flag when reading binary files, so that the opposite conversion isn’t performed:

    ifstream file;
    file.open(filename.c_str(), ios::in | ios::binary);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to wrap my head around this for a while now
I have been trying to learn Python for a while now. By chance, I
I have been trying to get this working for quite a while now but
For a while now i have been trying to figure out the algorithms behind
I have been trying to solve this one for a while now and have
Hey I have been battling with this problem for a while now. Perhaps there
I have been trying for a while now trying to figure out how to
I have been using C# for a while now, and going back to C++
I have been using Ruby for a while now and I find, for bigger
Hello I have been having trouble with this for a while now. I have

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.