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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:33:33+00:00 2026-05-24T20:33:33+00:00

So, I’ve been playing around with c++ a bit and decided to write a

  • 0

So, I’ve been playing around with c++ a bit and decided to write a program that involves opening and writing to a file in binary mode. I am not too familiar with the iostream functionality of c++ (I mostly do API based programming), but I read several technical guides on the subject and wrote some code. The code is meant to open one file, read it’s data to a buffer, and then convert that buffer to another format and write it to another file. The problem is that it keeps throwing a “Debug Assertion” error which apparently revolves around the invalid use of a null pointer. However, I couldn’t make sense of it when I looked through the code. I probably just misused the iostream library or made a simple logic error. I need to have the separate SetMemBlock function as I plan on using the same base for formatting different output on a variety of functions. This is just my prototype. Anyways, here’s my quick n’ dirty class setup:

const DebugMode = true;

class A
{
public:
    bool FileFunction( const char *, const char * );

protected:
    bool SetMemBlock( char *, std::fstream &, std::streamoff & );

private:
    std::fstream SrcFileStream;
    std::fstream DestFileStream;
};

bool A::SetMemBlock( char* MemBlock, std::fstream & FileStream, std::streamoff & Size )
{
    std::streamoff TempOff = 0;
    //This is meant to check for a non-empty buffer and to see if the stream is valid.
    if( MemBlock != 0 || !FileStream.is_open() )
        return false;

    TempOff = FileStream.tellg();
    FileStream.seekg(0, std::ios::end);
    Size = FileStream.tellg();

    MemBlock = new( std::nothrow ) char[ (int) Size ];

    if( MemBlock == 0 )
        return false;

    FileStream.seekg(0, std::ios::beg);
    FileStream.read( MemBlock, (int) Size );

    if( !FileStream )
        return false;

    FileStream.seekg(TempOff);
    return true;
}

bool A::FileFunction( const char * SrcFile, const char * DestFile )
{
    char * MemBlock = 0;
    std::streamoff Size = 0;

    SrcFileStream.open( SrcFile, std::ios::binary | std::ios::in );
    DestFileStream.open( DestFile, std::ios::binary | std::ios::out );

    if( !SrcFileStream.is_open() || !DestFileStream.is_open() )
        return false;

    if( DebugMode )
    {
        std::cout<<"Files opened succesfully...\nNow writing memory block..."<<std::endl;
    }

    if( !SetMemBlock( MemBlock, SrcFileStream, Size ) )
    {
        std::cout<<"An error occured when reading to memory block!"<<std::endl;
        return false;
    }

    if( DebugMode )
    {
        std::cout<<"Memory block written..."<<std::endl;
    }

    DestFileStream.seekp( std::ios::beg );
    DestFileStream.write( MemBlock, Size );

    SrcFileStream.close();
    DestFileStream.close();
    delete[] MemBlock;
    return true;

}
  • 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-24T20:33:34+00:00Added an answer on May 24, 2026 at 8:33 pm

    You’re passing MemBlock to SetMemBlock by value. The function therefore just sets the value of a local copy, which has no effect on the calling function; the value of MemBlock in the calling function thus remains garbage. Using it as a pointer will probably then lead to an assertion (if you’re lucky) or an out-and-out crash (if you’re not.) You want to pass that argument by reference instead.

    If you don’t know what these terms mean, Google “pass by value” and “pass by reference”. You really need to understand the difference!

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string

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.