C
Cliff Martin
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;
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;
}
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;
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;
}