// =======================================
// Windows-2000
// GNU g++ version 3.3.1 (cygming special)
// =======================================
// ############# C++ Code : BEGIN #############
#include <cassert>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <algorithm>
#include <fstream>
using namespace std;
#define COUT cout << "[ " << __FILE__ << ", " << setw(3) << right <<
__LINE__ << " ] "
#define FATAL_MSG(t) cout << endl; COUT << "FATAL ERROR : " << t <<
endl
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ==============================
bool read_file (const string& filename_i, vector<vector<string> >&
data_matrix_o);
string get_row (const vector<string>& data_vector_i);
vector<string> get_rows (const vector<vector<string> >& data_matrix_i);
void show_file_data (const string& filename_i);
// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// ==============================
string get_row (const vector<string>& data_vector_i)
{
ostringstream oss;
copy (data_vector_i.begin(), data_vector_i.end(),
ostream_iterator<string> (oss, " "));
return oss.str();
}
// ==============================
vector<string> get_rows (const vector<vector<string> >& data_matrix_i)
{
vector<string> ret_vector;
transform (data_matrix_i.begin(), data_matrix_i.end(), back_inserter
(ret_vector), get_row); // --- Variant#1 ---
#if (0) // --- Variant#2 ---
for (size_t i = 0; i < data_matrix_i.size(); i++)
{
ostringstream oss;
copy (data_matrix_i[i].begin(), data_matrix_i[i].end(),
ostream_iterator<string> (oss, " "));
ret_vector.push_back (oss.str());
}
#endif
return ret_vector;
}
// ==============================
void show_file_data (const string& filename_i)
{
vector<vector<string> > data;
const size_t setw1 = 23;
if (!read_file (filename_i, data))
{
FATAL_MSG ("Unable to read file <" << filename_i << ">");
return;
}
// ----------------------------------
cout << endl;
cout << "\t" << string (setw1, '-') << endl;
cout << "\t--- File " << filename_i << endl;
cout << "\t" << string (setw1, '-') << endl;
vector<string> rows (get_rows (data));
for (size_t i = 0; i < rows.size(); i++)
{
cout << "\t Row#" << setw(3) << (i + 1) << " ---> " << rows[i] <<
endl;
}
cout << "\t" << string (setw1, '-') << endl;
cout << endl;
}
// ==============================
bool read_file (const string& filename_i, vector<vector<string> >&
data_matrix_o)
{
assert (!filename_i.empty());
ifstream fin (filename_i.c_str());
if (!fin)
{
FATAL_MSG ("Unable to open file <" << filename_i << ">");
return false;
}
assert (fin.is_open());
string file_line;
data_matrix_o.clear();
while (getline (fin, file_line))
{
istringstream iss (file_line.c_str ());
istream_iterator<string> b(iss), e;
data_matrix_o.push_back(vector<string> (b, e)); // Variant#1
#if (0) // --- Variant#2 ---
data_matrix_o.push_back(vector<string>());
copy (b, e, back_inserter (data_matrix_o.back()));
#endif
}
fin.close();
assert (!fin || (fin && !fin.is_open()));
return true;
} // read_file
// ==============================
int main (int argc, char** argv)
{
string file_line;
cout << endl;
cout << " YOUR COMMAND LINE :";
for (size_t i = 0; i < argc; i++) cout << " " << argv[i];
cout << endl;
cout << endl;
const bool argc_condition = (argc > 1);
if (!argc_condition)
{
FATAL_MSG (""
<< " Invalid command line"
<< endl
<< " USAGE : "
<< argv[0]
<< " <Data-File-Name> [<Data-File-Name> [...]] "
<< endl
<< " EXAMPLE : "
<< argv[0]
<< " "
<< "file1.txt"
<< " "
<< "file2.cpp"
<< " "
<< "file3.h"
<< endl
);
return 1;
}
assert (argc_condition);
for (size_t i = 1; i < argc; i++) show_file_data (argv[i]);
return 0;
}
--
Alex Vinokur
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn