Solved

Basic Question: Outputting to streams

Posted on 2004-10-13
3
207 Views
Last Modified: 2010-04-01
Hello, I'm writing a tiny Console App which can either output to screen or a file. I'm new to C/C++ although I'm fairly good at HTML/PHP/VB/etc.. so I understand programming concepts, just not anything to do with C


At the minute I'm having to use:

if(fileoutput==0) {
      printf("%c", letter);
} else {
      fputc(letter, outstream);
}

Is ther a nice way to check a cariable and use ONE command to ouypuy the desired character? letter is, in this case a char and outstream is a file (IF using file output, otherwise it's unset) (This probably obvious to everyone, but...)

How can I make this nicer?
0
Comment
Question by:basiclife
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 17

Accepted Solution

by:
rstaveley earned 250 total points
ID: 12298038
Here's how to do it in C:

--------8<--------
#include <stdio.h>

FILE *whatever = stdout;

void display_char(char c)
{
      fputc(c,whatever);
}

int main()
{
FILE *fout;
const char msg[] = "Hello, world\n",*str;

      fout = fopen("x.txt","w");

      whatever = stdout;
      for (str = msg;*str;++str)
            display_char(*str);

      whatever = fout;
      for (str = msg;*str;++str)
            display_char(*str);

      whatever = stderr;
      for (str = msg;*str;++str)
            display_char(*str);

      fclose(fout);
}
--------8<--------
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 12298821
Or C++:

#include <iostream>
#include <fstream>
using namespace std;

void main(int nArgs, char* pszArgs[])
{
   ofstream ofs;
   ostream  couts = cout;   // save old cout
   if (nArgs > 1)
   {
       ofs.open(pszArgs[1]);
       if (ofs)
           cout = ofs;
   }  
   cout << "Any you want " << endl;

   if (nArgs > 1)
   {
        ofs.close();
        cout = couts; // get saved back
   }
}

Regards, Alex



0
 
LVL 15

Expert Comment

by:efn
ID: 12304637
Assigning and copying stream objects may work, but may not be portable.  It's safer to fiddle with the stream buffer, as described in:

http://www.icce.rug.nl/documents/cplusplus/cplusplus05.html#l91

Other references:

http://www.langer.camelot.de/Articles/Papers/IOStreams/IOStreams.htm

http://www.roguewave.com/support/docs/sourcepro/stdlibug/34-2.html
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

705 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