using fprintf to write to a file...

Posted on 2004-12-01
Last Modified: 2010-04-15
Ive inherited some code which I have modified and it works fiine!

Essentially, it reads in a stream from stdin, does stuff and writes it out to a new user defined stream.
Ive used streams just because I thought it would give me most flexibility for future development.

I wrapped my C binary in a shell wrapper and redirected my stream output by using  binary_name 6>file.out

For operational reasons I need to change that so that instead of sending my fprintf to my stream, I send it straight to the file.

That wouldnt seem to be an insummountable problem but I cant seem to do it!!

Can anyone help?  I thought it should be easy for me to change fprintf(STREAM,string) to fprintf(FILE,string) because I thought an open file took a stream as its input anyway, so in essence I should just be able to somehow point my stream to the file?


Question by:stummj

    Author Comment

    If anyone needs to chat to get clarifications I am on msn right now
    LVL 86

    Assisted Solution

    What about using 'freopen()', e.g. like

    /* FREOPEN.C: This program reassigns stderr to the file
     * named FREOPEN.OUT and writes a line to that file.

    #include <stdio.h>
    #include <stdlib.h>

    FILE *stream;

    void main( void )
       /* Reassign "stderr" to "freopen.out": */
       stream = freopen( "freopen.out", "w", stderr );

       if( stream == NULL )
          fprintf( stdout, "error on freopen\n" );
          fprintf( stream, "This will go to the file 'freopen.out'\n" );
          fprintf( stdout, "successfully reassigned\n" );
          fclose( stream );
       system( "type freopen.out" );
    LVL 11

    Expert Comment

    show what was the original code and what you changed it to ..

    only the modified part should be sufficient I thin ....

    Author Comment

    The original code just wrote to the stdout stream. I just copy and pasted really, and wrote to my custom stream instead.
    LVL 11

    Accepted Solution

    say you had

    fprintf(stdout, "xyz");

    you can put the following code in its place

    File *fp;

    fp = fopen("newfile.txt",  "w" );
    if(fp == NULL ) { print error and exit or whatever suits you }

    fprintf(fp, "xyz");

    fclose(fp);   // when yuo are done close the file

    Author Comment

    Thanks both.

    I simply had to change:



    stream = fopen(filename,type)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
    This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
    The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
    The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now