Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Assign cout or cin to a filehandle

Reply
Thread Tools

Assign cout or cin to a filehandle

 
 
Cliff Martin
Guest
Posts: n/a
 
      02-08-2007
Hi,

I am writing a simple filter type program that can take input from
file or stdin, and output to a file or stdout. It would make life much
easier if I could assign cin to a ifstream object and read it like a
file and do the same for cout (to an ofstream). Any ideas how I could
do this? Or perhaps some comments on how I could redo the code to do
what I want? Simplified code follows:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>

using std::cout;
using std::endl;
using std::string;

void usage(void);

int main(int argc, char* argv[]) {
string fields;

string line;
std::ifstream infile;
std:fstream outfile;
for (int i=0; i<argc; i++) {
string option = argv[i];
std::transform(option.begin(), option.end(), option.begin(),
tolower);

if ((option == "-i") || (option == "--infile"))
{
if (argc > i) {
infile.open(argv[++i]);
if (infile.fail()) {
std::cerr << "Could not open file for reading!\n";
exit(EXIT_FAILURE);
}
}
}
else if ((option == "-o") || (option == "--outfile")) {
if (argc > i)
outfile.open(argv[++i]);
if (outfile.fail()) {
std::cerr << "Could not open file for writing!\n";
exit(EXIT_FAILURE);
}
}
}

std::vector<string> lines;
if (infile.is_open()) {
while(getline(infile, line)) {
lines.push_back(line);
}

infile.close();
}
else {
while(getline(std::cin, line)) {
lines.push_back(line);
}
}

std::vector<string>::iterator lines_iter;
const std::vector<string>::iterator lines_end=lines.end();
if (outfile.is_open()) {
for(lines_iter=lines.begin(); lines_iter!=lines_end; +
+lines_iter)
outfile << *lines_iter << endl;

outfile.close();
}
else {
for(lines_iter=lines.begin(); lines_iter!=lines_end; +
+lines_iter)
cout << *lines_iter << endl;
}

return EXIT_SUCCESS;
}

 
Reply With Quote
 
 
 
 
Pete Becker
Guest
Posts: n/a
 
      02-08-2007
Cliff Martin wrote:
>
> I am writing a simple filter type program that can take input from
> file or stdin, and output to a file or stdout. It would make life much
> easier if I could assign cin to a ifstream object and read it like a
> file and do the same for cout (to an ofstream). Any ideas how I could
> do this?


Nope. But that's not the best approach. The type of the standard input
stream is a class derived from std::istream, and the types of the
standard output streams are derived from std:stream. Similarly,
std::ifstream is derived from std::istream, and std:fstream is derived
from std:stream. So write your I/O functions to take istreams and
ostreams by reference, and call them with the appropriate object.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
 
 
 
Cliff Martin
Guest
Posts: n/a
 
      02-09-2007
> from std:stream. So write your I/O functions to take istreams and
> ostreams by reference, and call them with the appropriate object.
>
> -- Pete


That worked, thanks. I open the files pass them in to a function as an
istream or ostream reference. If the filename was not provided for
either, I pass in std::cout or std::cin.

so with a function like this:
process_file(std::istream &infile, std:stream &outfile)

I can call it like this:

process_file(ifstream_filehandle, ofstream_filehandle);

or

process_file(ifstream_filehandle, std::cout);

or

process_file(std::cin, std::cout)

Just what I was looking for. Thanks again.

Cliff

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      02-09-2007
Cliff Martin wrote:
>
> Just what I was looking for. Thanks again.
>


That's the power of object-oriented programming.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
 
Reply With Quote
 
Sylvester Hesp
Guest
Posts: n/a
 
      02-09-2007

"Cliff Martin" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi,
>
> I am writing a simple filter type program that can take input from
> file or stdin, and output to a file or stdout. It would make life much
> easier if I could assign cin to a ifstream object and read it like a
> file and do the same for cout (to an ofstream). Any ideas how I could
> do this?


Pete already showed you the better approach, but it is possible to "assign"
a stream to another. I used quotation marks because you're not really
assigning them, rather you're replacing the underlaying stream buffers that
are responsible for the actual IO of the streams:

#include <iostream>
#include <fstream>

int main()
{
// create a new streambuf object that writes to a file
std::filebuf outFileBuf("myfile.txt", std::ios:ut);

// set the streambuf object to cout and store the previous streambuf
std::streambuf * oldBuf = std::cout.rdbuf(&outFileBuf);

std::cout << "This writes to myfile.txt rather than stdout" <<
std::endl;

// restore original streambuf
std::cout.rdbuf(oldBuf);
}


 
Reply With Quote
 
=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
Posts: n/a
 
      02-09-2007
Cliff Martin wrote:

> I am writing a simple filter type program that can take input from
> file or stdin, and output to a file or stdout. It would make life much
> easier if I could assign cin to a ifstream object and read it like a
> file and do the same for cout (to an ofstream). Any ideas how I could
> do this? Or perhaps some comments on how I could redo the code to do
> what I want? Simplified code follows:


You can use an ofstream and a ifstream. Create each of them without
arguments and, depending on the situation, open the file or use rdbuf to
make the underlying stream use the streambuf of cin or cout.

--
Salu2
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Using cin.get() and cin.eof() ends up in an endless loop Fernando C++ 4 11-16-2011 07:19 PM
cin and cin.getline() Aleander C++ 5 03-06-2005 03:57 PM
object to standard output and input ( cout / cin ) gouki C++ 3 08-06-2004 05:10 AM
std::cin.ignore() and std::cin.clear() Chris Mantoulidis C++ 5 01-06-2004 11:08 PM
cin and cin.clear() problem TaiwanNoWhere C++ 8 10-17-2003 05:53 PM



Advertisments