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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:28:25+00:00 2026-06-06T19:28:25+00:00

I’m using fwrite in an MFC application to print content of lBuffer as shown

  • 0

I’m using fwrite in an MFC application to print content of lBuffer as shown in following C++ code:

PvBuffer *lBuffer = NULL;

// Retrieve next buffer     
PvResult lResult = lStream.RetrieveBuffer(&lBuffer, &lOperationResult, 1000);
if (lResult.IsOK())
{
    FILE *fp = fopen("C:\\Users\\acm45\\Desktop\\abuffer.bin", "wb");
    fwrite(lBuffer, 1, 10075968, fp);
    fclose(fp);
}

Any idea why the content of the file abuffer.bin is always empty even though IResult returns success?
Note the file is always created when I run the program, but it is empty and size is 0KB.

Update:
To debug I did this:

FILE *fp = fopen("C:\\Users\\acm45\\Desktop\\abuffer.bin", "wb");
if (fp) {
    fwrite(lBuffer, 1,10075968, fp);
    fclose(fp);
} else {
    printf("error opening file");
}

and the output did not print “error opening file”, but still the file is empty. What do I do now?

  • 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-06T19:28:26+00:00Added an answer on June 6, 2026 at 7:28 pm

    I guess you are having some undefined behaviour there.

    The man page of fwrite says:

    size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

    The function fwrite() writes nmemb elements of data, each size bytes long, to the stream pointed to by stream, obtaining them from the location given by ptr.

    But your call

    lStream.RetrieveBuffer( &lBuffer, &lOperationResult, 1000 );
    

    lets me guess you don’t have 10075968 elements with each one byte long.

    Also: Is your buffer a collection of POD elements? If not, that’s another reason for undefined behaviour. fwrite is only for POD types.

    Generally, it is better to use C++ streams.


    addendum to explain fwrite

    size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

    Here, size means the size of a single element.

    nmemb is the number of such elements. For example:

    ...
    struct Foobar {
        int frob;
    };
    
    int main () {
        ...
        Foobar thingies[5];
        fwrite (thingies, sizeof(Foobar), 5, some_file);
    }
    

    A more generic approach is this:

        Foobar thingies[5];
        const size_t nm = sizeof(thingies) / sizeof(Foobar);
        fwrite (thingies, sizeof(Foobar), nm, some_file);
    

    This read: "size of the array, divided by size of a single element, which equals the number of elements in that array".

    But note that this only works for arrays, not pointers!

        Foobar *thingies = new Foobar[5];
        const size_t nm = sizeof(thingies) / sizeof(Foobar);
        fwrite (thingies, sizeof(Foobar), nm, some_file);
        // THIS IS WRONG. sizeof(thingies) now equals to the
        // size of a pointer!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.