Solved

Basic Question: Outputting to streams

Posted on 2004-10-13
3
202 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
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 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.

772 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

11 Experts available now in Live!

Get 1:1 Help Now