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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:47:26+00:00 2026-05-13T22:47:26+00:00

In C/C++, there is a ‘write() function which let me write to either file

  • 0

In C/C++, there is a ‘write() function which let me write to either file or a socket, I just pass in the file descriptor accordingly). And there is a fprintf() which allow me to do fprintf (myFile, “hello %d”, name); but it only works for file.

Is there any api which allows me to do both?
i.e. able to let me do print formatting and able to switch between writing to file or socket?

Thank you.

  • 1 1 Answer
  • 1 View
  • 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-13T22:47:26+00:00Added an answer on May 13, 2026 at 10:47 pm

    You can use sprintf or snprintf to print to a char * buffer, and then use write. To get a file descriptor from a FILE * variable, you can use fileno. There is no portable way to go from a file descriptor to a FILE *, though: you can portably to use fdopen to associate a FILE * with a valid file descriptor.

    In addition, the latest POSIX standard specifies dprintf, but the GNU libc dprintf man page has this to say:

    These functions are GNU extensions, not in C or POSIX. Clearly, the
    names were badly chosen. Many systems (like MacOS) have incompatible
    functions called dprintf(), usually some debugging version of printf(),
    perhaps with a prototype like

    void dprintf (int level, const char *format, ...);
    

    where the first parameter is a debugging level (and output is to
    stderr). Moreover, dprintf() (or DPRINTF) is also a popular macro name
    for a debugging printf. So, probably, it is better to avoid this function in programs intended to be portable.

    Of course, the libc manual page is not updated with the latest standard in mind, but you still have to be careful with using dprintf, since you might get something you don’t want. 🙂

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

Sidebar

Related Questions

There's a Rails 3.2.3 web application which doesn't use any database. But in spite
There is a directed graph having a single designated node called root from which
There are two intents on the receiver side which are called from the same
I have just tried to save a simple *.rtf file with some websites and
There are several shading languages available today like GLSL, HLSL, CG, which one to
There's a bug in FireBug: I accidentally clicked where the line numbers are, which
There is a div (form) which opens on the click of a button. There
There is a text file with 1,000,000 lines of code floating around and Ctrl+F
There is an application written in PHP which I am converting to Ruby. When
There is a term for this that I just cannot think of, and I'm

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.