Solved

Basic Question: Outputting to streams

Posted on 2004-10-13
3
203 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 how to clear a vector as well as how to detect empty vectors in C++.

920 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

12 Experts available now in Live!

Get 1:1 Help Now