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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T05:52:21+00:00 2026-05-30T05:52:21+00:00

I am working with some legacy code which uses something like this: void store_data(FILE

  • 0

I am working with some legacy code which uses something like this:

void store_data(FILE *file);

However, I don’t want to store data on the disk, I want to store it in memory (char *buf). I could edit all of the code, but the code jumps all over the place and fwrite gets called on the file all over the place. So is there an easier way, for example that I can map a FILE* object to an (auto-growing) buffer? I do not know the total size of the data before-hand.

The solution has to be portable.

  • 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-30T05:52:22+00:00Added an answer on May 30, 2026 at 5:52 am

    There is no way to do this using only the facilities provided in the C standard. The closest you can come is

    FILE *scratch = tmpfile();
    ...
    store_data(scratch);
    ...
    /* after you're completely done calling the legacy code */
    rewind(scratch);
    buf = read_into_memory_buffer(scratch);
    fclose(scratch);
    

    This does hit the disk, at least potentially, but I’d say it’s your best bet if you need wide portability and can’t modify the “legacy code”.

    In POSIX.1-2008, there is open_memstream, which does exactly what you want; however, this revision of POSIX is not yet widely adopted. GNU libc (used on Linux and a few others) has it, but it’s not available on OSX or the *BSDs as far as I know, and certainly not on Windows.

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

Sidebar

Related Questions

I am working on some legacy code which opens a file and adds binary
I have been working on some legacy C++ code that uses variable length structures
I'm working with a legacy piece of code in some stuff for work which
I'm working with some legacy test code which makes use of a TestSetup class
I am working on an application which has some legacy code. Here, there is
I'm working with some legacy code and I came across a function which is
I'm working on some legacy code and I ran into something that I'm not
We are working with some legacy code that accesses a shared drive by the
I am working with some legacy code, and at some point there is a
I've been working with some C# legacy code and I've been seeing a lot

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.