Solved

Basic Question: Outputting to streams

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cross compile release version of c++ program for linux 2 132
sorting efficency of sorting algorithm 30 113
Header of docx file 17 102
Autosar OS Multicore Share Resources confusion ? 2 38
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

773 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