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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:57:52+00:00 2026-05-12T07:57:52+00:00

I have a bad feeling that the answer to this question is no, but

  • 0

I have a bad feeling that the answer to this question is “no”, but I wanted to throw this out there in case anyone has any clever ideas.

I have a set of output routines that take a complex data structure and print it in a textual format. They have prototypes like:

void print_mystruct(struct mystruct *s, FILE *stream)

I wrote it this way so that I can get efficient, buffered output to the terminal, to a file, to the network, etc.

Unfortunately, there’s no way that I know of, using standard C99, that I can use these same routines to build up a string in memory.

So my questions are:

  1. is there any clever way that I can efficiently use fputs(), fprintf(), etc. to output to a string?
  2. if not, is there a better paradigm that can efficiently do both buffered file output and string building? The best I can think of is to have my own structure with a vtable (instead of a FILE*). The vtable would have an output function that would either append to a string or call fwrite. But then I’d have to create printf wrappers also.

Any other clever ideas?

EDIT: I have discovered that fmemopen() is part of POSIX.1-2008 (see: fmemopen()) but is not widely supported, at least according to the GNU libc manpage.

  • 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-12T07:57:53+00:00Added an answer on May 12, 2026 at 7:57 am

    There’s no portable way of doing this. glibc systems(linux) have open_memstream/fmemopen , other systems might not have something like it.

    The portable way is to write to a file and read it back into a string. , or to separate the concerns. Instead of implementing

    void print_mystruct(struct mystruct *s,FILE *f);
    

    You’d e.g. implement

    char *mystruct_2_str(struct mystruct *s);
    

    Which dynamically allocates a string(Or pass in a buffer), formats it to a string with standard string functions (snprintf etc.) and have the caller decide whether to write that to a FILE*

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

Sidebar

Related Questions

I have a bad feeling that the answer is no, but is the checkin
We have some applications that sometimes get into a bad state, but only in
I have a very bad feeling about using lock in my code but now
I have the feeling that I'm missing the obvious, but have not succeeded with
i hat one task, and know i have to continue but have a bad
I become use raw socket and i have this problem: bad file descriptor the
Is it bad policy to have lots of work classes(such as Strategy classes), that
I have read multiple articles about why singletons are bad. I know it has
My feeling says it's not posible but anyway I am curious if there is
I have a table that must reference another record, but of the same table.

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.