Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[C++] Redirecting cin <--> file

7 views
Skip to first unread message

Alex Vinokur

unread,
Mar 24, 2003, 12:09:19 AM3/24/03
to
// ===============================================
// [C++] Redirecting cin <--> file
// Sample of usage
// -----------------------------------------------
// Alex Vinokur
// mailto:ale...@connect.to
// http://www.simtel.net/pub/oth/19088.html
// -----------------------------------------------
// 2003-03-24
// ===============================================


// =======================
// Windows 2000
// MinGW 2.0.0-2
// GNU gcc/g++ version 3.2
// =======================


// ==================
// File redir_in.cpp
// ==================


#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <assert.h>
using namespace std;


// ------------
void sample (const string& infile_name_i);
// ------------

#define SHOW_DATA(t, x) \
cout << "" \
<< t \
<< " ---> " \
<< "[ " \
<< __FILE__ \
<< ", " \
<< setw(3) \
<< __LINE__ \
<< " ] " \
<< #x \
<< " = " \
<< x \
<< endl

#define SHOW_CIN_DATA(x) SHOW_DATA("cin ", x)
#define SHOW_FILE_DATA(x) SHOW_DATA("file", x)

// ------------
void sample (const string& infile_name_i)
{
streambuf* save_sbuf_fin;
streambuf* save_sbuf_cin;
ifstream fin;
int intdata_in;

const string empty_prefix (5, ' ');
const string enter_txt ("Enter ");
const string read_txt ("Read ");
const string via_cin_txt ("via cin ");
const string via_file_txt ("via file ");
const string from_file_txt ("from file ");

const string enter_via_cin_txt1 (empty_prefix + enter_txt + via_cin_txt + ": ");
const string enter_via_cin_txt2 (empty_prefix + read_txt + from_file_txt + infile_name_i + " " + via_cin_txt);
const string enter_via_file_txt (empty_prefix + enter_txt + via_file_txt + infile_name_i + " : ");
const string read_from_file_txt (empty_prefix + read_txt + from_file_txt + infile_name_i);


fin.open (infile_name_i.c_str());
assert (fin);
assert (fin.is_open());

save_sbuf_cin = cin.rdbuf();
assert (save_sbuf_cin);

save_sbuf_fin = fin.rdbuf();
assert (save_sbuf_fin);


// ------ 1 ------
cout << endl << "---------" << " 1. No redirecting " << "---------" << endl;

cout << enter_via_cin_txt1;
intdata_in = 0;
cin >> intdata_in;
SHOW_CIN_DATA (intdata_in);

cout << read_from_file_txt << endl;
intdata_in = 0;
fin >> intdata_in;
SHOW_FILE_DATA (intdata_in);
// ---------------

// ------ 2 ------
cout << endl << "---------" << " 2. Redirecting cin to file" << "---------" << endl;
cin.rdbuf(save_sbuf_fin);

cout << enter_via_cin_txt2 << endl;
intdata_in = 0;
cin.clear ();
cin.seekg (0);
cin >> intdata_in;
SHOW_CIN_DATA (intdata_in);

cout << read_from_file_txt << endl;
intdata_in = 0;
fin.clear ();
fin.seekg (0);
fin >> intdata_in;
SHOW_FILE_DATA (intdata_in);

cin.rdbuf(save_sbuf_cin);
// ---------------


// ------ 3 ------
cout << endl << "---------" << " 3. No redirecting " << "---------" << endl;

cout << enter_via_cin_txt1;
intdata_in = 0;
cin >> intdata_in;
SHOW_CIN_DATA (intdata_in);

cout << read_from_file_txt << endl;
intdata_in = 0;
fin.clear ();
fin.seekg (0);
fin >> intdata_in;
SHOW_FILE_DATA (intdata_in);
// ---------------


// ------ 4 ------
cout << endl << "---------" << " 4. Redirecting file to cin" << "---------" << endl;
fin.istream::rdbuf(save_sbuf_cin);

cout << enter_via_cin_txt1;
intdata_in = 0;
cin >> intdata_in;
SHOW_CIN_DATA (intdata_in);

cout << enter_via_file_txt;
intdata_in = 0;
fin >> intdata_in;
SHOW_FILE_DATA (intdata_in);

fin.istream::rdbuf(save_sbuf_fin);
// ---------------


// ------ 5 ------
cout << endl << "---------" << " 5. No redirecting " << "---------" << endl;

cout << enter_via_cin_txt1;
intdata_in = 0;
cin >> intdata_in;
SHOW_CIN_DATA (intdata_in);

cout << read_from_file_txt << endl;
intdata_in = 0;
fin.clear ();
fin.seekg (0);
fin >> intdata_in;
SHOW_FILE_DATA (intdata_in);
// ---------------

fin.close();
assert (!fin || (fin && !fin.is_open()));

}


// ------------
int main (int argc, char** argv)
{
if (!(argc > 1))
{
cout << "USAGE : " << argv[0] << " <input-file-name>" << endl;
return 1;
}
sample(argv[1]);

return 0;
}


0 new messages