Passing a stream?

This is a discussion on Passing a stream? within the C Programming forums, part of the General Programming Boards category; Hey all, I was wondering if it's possible to pass a stream to a function? e.g. void print_stuff(int i, STREAM ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    40

    Passing a stream?

    Hey all,

    I was wondering if it's possible to pass a stream to a function?

    e.g.

    void print_stuff(int i, STREAM s)
    {
    sprintf(s, "%d", i);
    }

    This is because I've got a function that I'd like to be able to use to print to the screen sometimes, and some other times to a different stream. I know I can just make a separate function for it, but it seems like this would be handy to do.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, you can pass a stream pointer.

    The datatype is FILE*; stdin, stdout, and stderr are all predefined FILE (stream) pointers.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    23,820
    Pass a pointer to FILE.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    40
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Arguments
    By bolivartech in forum C Programming
    Replies: 13
    Last Post: 10-15-2009, 01:31 PM
  2. Closing a stream
    By cunnus88 in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2008, 04:15 PM
  3. Passing by reference not always the best
    By franziss in forum C++ Programming
    Replies: 3
    Last Post: 10-26-2005, 07:08 PM
  4. Help! About text stream and binary stream
    By Antigloss in forum C Programming
    Replies: 1
    Last Post: 09-01-2005, 08:40 AM
  5. Passing file stream as function parameter
    By simonc2 in forum C++ Programming
    Replies: 6
    Last Post: 12-22-2004, 11:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21