?
Solved

Basic Question: Outputting to streams

Posted on 2004-10-13
3
Medium Priority
?
208 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 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