Is there a good idiom to deal with alternative output streams? - c++

I want to write a simple program that depending on the options passed it the executable will print the output to the screen or to a file. The program is simple.
#include<iostream>
int main(int argc, char* argv[]){
... process options...
std::ostream& out = ... // maybe std::cout, maybe a *new* std::ofstream;
out << "content\n";
}
Is there a good idiom to make out refer alternatively to std::cout or a file stream at runtime?
I tried with pointers, but it is horrible. I couldn't avoid using pointers (Not to mention that more ugly code is needed to delete the pointer later).
#include<iostream>
#include<ofstream>
int main(int argc, char* argv[]){
std::string file = argc>1?argv[1]:"";
std::clog << "file: " << file << '\n';
// if there is no argument it will print to screen
std::ostream* out = (file=="")?&std::cout:(new std::ofstream(file)); // horrible code
*out << "content" << std::endl;
if(out != &std::cout) delete out;
}
I don't know, perhaps there is some feature of C++ streams that allows this. Perhaps I have to use some kind of type erasure. The problem, I think, is that std::cout is something that already exists (is global), but std::ofstream is something that has to be created.
I managed to use open and avoid pointers but it is still ugly:
int main(int argc, char* argv[]){
std::string file = argc>1?argv[1]:"";
std::clog << "file: " << file << '\n';
std::ofstream ofs;
if(file != "") ofs.open(file);
std::ostream& out = (file=="")?std::cout:ofs;
out << "content" << std::endl;
}

My preference is to use streams with suitable stream buffers installed. Here is one way direct output to a file or to std::cout:
#include <iostream>
#include <fstream>
int main(int ac, char* av) {
std::ofstream ofs;
if (1 < ac) {
ofs.open(av[1]);
// handle errors opening the file here
}
std::ostream os(file? file.rdbuf(): std::cout.rdbuf());
// use os ...
}

So much over-engineering.
#include <iostream>
#include <fstream>
int main(int argc, char* argv[]) {
std::ofstream ofs(argc > 1 ? argv[1] : "");
std::ostream& os = ofs.is_open() ? ofs : std::cout;
// use os ...
}

A runtime binding of the desired stream will pretty much need to look like what you already have.
On the pointer issue, sure you can clean it up a bit... maybe something like this? This is assuming you only want to create the ofstream if the argument exists.
int main(int argc, char* argv[]){
std::string file = argc > 1 ? argv[1] : "";
std::clog << "file: " << file << '\n';
// if there is no argument it will print to screen
std::unique_ptr<std::ostream> fp;
if (file == "")
fp = std::make_unique<std::ofstream>(file);
std::ostream& out = (fp && fp->is_open()) ? std::cout : *fp; // not so horrible code
out << "content" << std::endl;
}
If the dynamic object is not required, the easiest may be something list this;
int main(int argc, char* argv[]){
std::string filename = (argc > 1) ? argv[1] : "";
std::ofstream file(filename);
// if there is no argument (file) it will print to screen
std::ostream& out = file.is_open() ? file : std::cout;
out << "content" << std::endl;
}

I often use something like this for command-line tools:
int main(int, char* argv[])
{
std::string filename;
// args processing ... set filename from command line if present
if(argv[1])
filename = argv[1];
std::ofstream ofs;
// if a filename was given try to open
if(!filename.empty())
ofs.open(filename);
// bad ofs means tried to open but failed
if(!ofs)
{
std::cerr << "Error opeing file: " << filename << '\n';
return EXIT_FAILURE;
}
// Here either ofs is open or a filename was not provided (use std::cout)
std::ostream& os = ofs.is_open() ? ofs : std::cout;
// write to output
os << "Some stuff" << '\n';
return EXIT_SUCCESS;
}

You could use a shared pointer to a stream for the polymorphic behavior:
#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>
void nodelete(void*) {}
std::shared_ptr<std::ostream> out_screen_stream() { return std::shared_ptr<std::ostream>(&std::cout, nodelete); }
std::shared_ptr<std::ostream> out_file_stream() { return std::make_shared<std::ofstream>(); }
std::shared_ptr<std::ostream> out_string_stream() { return std::make_shared<std::ostringstream>(); }
int main ()
{
std::shared_ptr<std::ostream> out;
// case condition:
out = out_screen_stream();
out = out_file_stream();
out = out_string_stream();
*out << "content" << std::endl;
return 0;
}
Note: A std::shared_ptr allows managing different possible streams, where some streams should not get deleted (e.g.: std::cout).
Similar, but with std::unique_ptr:
#include <memory>
#include <fstream>
#include <sstream>
#include <iostream>
class Deleter
{
public:
Deleter(bool use_delete = true) : use_delete(use_delete) {}
template <typename T>
void operator () (const T* p) {
if(use_delete)
delete p;
}
bool nodelete() const { return ! use_delete; }
private:
bool use_delete;
};
using unique_ostream_ptr = std::unique_ptr<std::ostream, Deleter>;
unique_ostream_ptr out_screen_stream() { return unique_ostream_ptr(&std::cout, false); }
unique_ostream_ptr out_file_stream() { return unique_ostream_ptr{ new std::ofstream }; }
unique_ostream_ptr out_string_stream() { return unique_ostream_ptr{ new std::ostringstream }; }
int main ()
{
unique_ostream_ptr out;
// case condition:
out = out_screen_stream();
out = out_file_stream();
out = out_string_stream();
*out << "content" << std::endl;
return 0;
}

Maybe a reference?
#include<iostream>
#include<ofstream>
int main(int argc, char* argv[])
{
auto &out = std::cout;
std::ofstream outFile;
std::string fileName = argc>1?argv[1]:"";
std::clog << "file: " << file << '\n';
// if there is no argument it will print to screen
if(!fileName.empty())
{
outFile.open(fileName);
out = outFile;
}
out<<"one, one, two";
return 0;
}

Related

Are there drawbacks to using a lambda and macro for error handling?

Properly testing function return values is fundamental, but it can quickly clutter the code and make it hard to read, like in the simple example below:
#include <iostream>
#include <fstream>
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
std::ifstream ifs(filename.c_str());
if (!ifs.is_open())
{
std::cerr << "Failed to open file " << filename << std::endl;
return 1;
}
ifs.close();
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
ifs.open(filename.c_str());
if (!ifs.is_open())
{
std::cerr << "Failed to open file " << filename << std::endl;
return 1;
}
return 0;
}
I have thought of a solution reducing cluttering by using a macro and c++11 lambda functions like this:
#include <iostream>
#include <fstream>
#define RETURN_IF(X,Y,Z) if ( X ) { Y ; return Z; }
auto open_file_error = [](const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
};
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
std::ifstream ifs(filename.c_str());
RETURN_IF (!ifs.is_open(), open_file_error(filename), 1 );
ifs.close();
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
ifs.open(filename.c_str());
RETURN_IF (!ifs.is_open(), open_file_error(filename), 1 );
return 0;
}
As you can see, the main function is less cluttered. Do you think that there are drawbacks to doing it like that or could it be a method to largely use?
Note that I use several macros to handle cases with or without a return value, for testing equality with a value, etc.
I propose the new version below to take into account two things:
- the answers and comments about the preference on using exceptions instead of return values;
- put away the emphasis on std::ifstream specific errors which are not the subject of the question.
#include <iostream>
#include <fstream>
#include <exception>
class OurExceptionForTheExternalLibraryFailure : public std::exception {};
#define CLEANUP_AND_THROW_IF(X,Y,Z) if ( X ) { Y ; throw Z; }
/* Return true in case of succes and false otherwise */
bool anyExternalFunction(const std::string& aString)
{
std::ifstream ifs(aString.c_str());
if (ifs.is_open())
{
ifs.close();
return true;
}
else
{
return false;
}
}
auto this_external_function_error_cleanup = [](const std::string& aString)
{
std::cerr << "The external function failed " << aString << std::endl;
// other stuff
};
int main(int argc, char **argv)
{
try
{
std::string aString = "/usr/include/malloc.h";
bool functionResult = anyExternalFunction(aString);
CLEANUP_AND_THROW_IF (!functionResult, this_external_function_error_cleanup(aString), OurExceptionForTheExternalLibraryFailure() );
std::cout << "Passed the first error handling" << std::endl;
aString = "/this/file/does/not/exist";
functionResult = anyExternalFunction(aString);
CLEANUP_AND_THROW_IF (!functionResult, this_external_function_error_cleanup(aString), OurExceptionForTheExternalLibraryFailure() );
}
catch (const OurExceptionForTheExternalLibraryFailure& e)
{
std::cerr << "Catched OurExceptionForTheExternalLibraryFailure. There was an error" << std::endl;
}
return 0;
}
What do you think about this new version (which still uses a macro, though...) ?
Well, if you are already using lambdas, and you don't want all that testing code everywhere, you could always do something like (NOTE: uncompiled/untested code,)
template <typename FileReader>
void with_file(std::string file, FileReader&& reader) {
std::ifstream in(file);
if (in) {
reader(in);
} else {
throw std::runtime_error("Failed to open file: " + file); // NOTE: I'm being lazy here
}
}
int main(...) {
with_file("foo.txt", [](auto& in) {
// do something with the stream
});
}
.. but it's a matter of preference, I like exceptions, lambdas and small utility functions, but some may not...
This is pretty much a textbook example of when to use exceptions.
You don't, however, have to write your own code to test for a file opening correctly, and throwing an exception (and so on) when it fails. Iostreams already support that fairly directly, so you can write code something like this:
#include <fstream>
#include <iostream>
int main() {
try {
std::ifstream in("/usr/include/malloc.h");
in.exceptions(std::ios::failbit);
in.close();
std::cout << "passed first test.\n";
std::ifstream in2("/this/file/does/not/exist");
in2.exceptions(std::ios::failbit);
in2.close();
std::cout << "Passed second test\n";
}
catch (std::system_error &f) {
std::cerr << "Failed to open file: " << f.what() << "\n";
}
}
Of course, if you want to get the try/catch out of main, you can do that as well. I'm not sure you gain much from doing so though.
More generally, however, exceptions are clearly the right tool for this job. For other functions that don't provide a way to get exceptions reported as exceptions, you may have to write a wrapper of your own. Either way, however, if you have a function that has some range of normal return values, and one (or a few) "special" values in indicate failure (and similar) that's a pretty decent indication that it's indicating an exceptional condition via the return value--and the right way to deal with exceptional conditions is via exceptions rather than return values.
Rather than try to reiterate the (long) list of why/when/how to use exception handling, I'll refer you (as a starting point) to Herb Sutter's old article on when and how to use exceptions.
Suggest this as a much cleaner example. NOW USING EXCEPTIONS...
I haven't tested that it's 100% the same behaviour as your example (which I appreciate is just that; an example).
By the way, MFC has a "SUCCESS" macro that does a similar check to your "RETURN_IF". I don't like that macro either...
#include <iostream>
#include <fstream>
#include <string>
void TestForFileOpen(const std::string& filename)
{
std::ifstream ifs(filename.c_str());
if (!ifs.is_open())
{
throw std::exception("Failed");
}
}
void ReportFileOpenFailure(const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
}
void NoisyTestForFileOpen(const std::string& filename)
{
try
{
TestForFileOpen(filename);
}
catch(...)
{
ReportFileOpenFailure(filename);
throw;
}
}
int main(int argc, char **argv)
{
std::string filename("/usr/include/malloc.h");
try
{
NoisyTestForFileOpen(filename);
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
NoisyTestForFileOpen(filename);
}
catch (...)
{
return 1;
}
return 0;
}
A more general example with a custom API:
#include <iostream>
#include <fstream>
#include <string>
class IFileTester
{
public:
virtual ~IFileTester() {}
// throws if file cannot be opened
virtual void TestForFileOpen(const std::string& filename) const = 0;
};
class IfStreamFileTester : public IFileTester // implement as many versions as you need
{
public:
virtual void TestForFileOpen(const std::string& filename) const
{
// implement this in terms of ifstream
std::ifstream ifs(filename.c_str());
// thanks #Jerry-Coffin
ifs.exceptions(std::ios::failbit);
}
};
void ReportFileOpenFailure(const std::string& filename)
{
std::cerr << "Failed to open file " << filename << std::endl;
}
void NoisyTestForFileOpen(const IFileTester& fileTester, const std::string& filename)
{
try
{
fileTester.TestForFileOpen(filename);
}
catch(...)
{
ReportFileOpenFailure(filename);
throw;
}
}
int main(int argc, char **argv)
{
IFileTester& fileTester = IfStreamFileTester();
std::string filename("/usr/include/malloc.h");
try
{
NoisyTestForFileOpen(fileTester, filename);
std::cout << "Passed the first error handling" << std::endl;
filename = "/this/file/does/not/exist";
NoisyTestForFileOpen(fileTester, filename);
}
catch (...)
{
return 1;
}
return 0;
}
Looks okay to me. I wouldn't put all your eggs into this basket because you don't have to make it much more complicated to run into limitations with preprocessor syntax but, as it is, this is fine.

Splitting read file C++

Lets start with that I have absolutely no experience with C++ , but I got this project to connect a POS with a verifone. We do not have the standard verifone SDK but something custom.
At fist I needed to prepair data to send to C++ and C++ will send it to the Verifone. This is where I am getting stuck, I have a .txt file, which I can read with C++ but now I need to split the data.
This is my current code:
#include "stdafx.h"
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
string file_get_contents(const char *filename)
{
ifstream in(filename);
if (in.fail())
{
cerr << "File not found: " << filename << endl;
return "";
}
std::stringstream buffer;
buffer << in.rdbuf();
in.close();
return buffer.str();
}
int main(int argc, char **argv)
{
vector<string> strings;
string contents = file_get_contents("C:/wamp/www/cmd/config.txt");
string s;
while (contents, s, '||') {
cout << s << endl;
strings.push_back(s);
}
cout << s; // ECHO CONTENTS
std::cin.ignore(); // pause
return 0;
}
With this code my console just stays blank, no data is being displayed.
The full string I am splitting is:
"notepad://amount=10320.53||session_id=7946548443287465/"
The result that I want is to get an array that uses "amount" and "session_id" as keys and their values as value.
What is the best way of achieving this?
I used the following code to actually display the string in my console which was working:
int main(int argc, char **argv)
{
string contents = file_get_contents("config.txt");
cout << contents; // ECHO CONTENTS
std::cin.ignore(); // pause
return 0;
}
This shows how to use a regex to extract the information you want, there are a lot of online resources on how to read files properly so I left that part out.
#include <iostream>
#include <regex>
#include <unordered_map>
#include <string>
int main(int argc, char **argv)
{
std::regex pattern("amount=([[:digit:]\\.]*)\\|\\|session_id=([[:digit:]]*)");
std::smatch results;
std::unordered_map<std::string, std::string> data;
std::string contents = "notepad://amount=10320.53||session_id=7946548443287465/";
//string contents = file_get_contents("C:/wamp/www/cmd/file.txt");
if(std::regex_search(contents, results, pattern))
{
data["amount"] = results[1];
data["session_id"] = results[2];
}
std::cout << "Amount: " << data["amount"] << std::endl;
std::cout << "Seesion ID: " << data["session_id"] << std::endl;
return 0;
}

Passing a file into a function

I'm trying to create a program that passes a file to a function. The function is supposed to detect how many lines are in my file. I don't think I'm passing the file correctly into my function, I've tried several different ways. Any help will be greatly appreciated.
#include <iostream>
#include <fstream>
#define die(errmsg) {cerr << errmsg << endl; exit(1);}
using namespace std;
int num_of_lines(ifstream file)
{
int cnt3;
string str;
while(getline(file, str))cnt3++;
return(cnt3);
}
int main(int argc, char **argv)
{
int num_of_lines(ifstream file);
string file;
file = argv[1];
if(argc == 1)die("usage: mywc your_file"); //for some reason not working
ifstream ifs;
ifs.open(file);
if(ifs.is_open())
{
int a;
cout << "File was opened\n";
a = num_of_lines(file);
cout <<"Lines: " << a << endl;
}
else
{
cerr <<"Could not open: " << file << endl;
exit(1);
}
ifs.close();
return(0);
}
Two problems with the function. First, you should pass the stream by reference. Second, you just forgot to initialise your counter.
int num_of_lines( ifstream &file )
{
int cnt3 = 0;
string str;
while( getline(file, str) ) cnt3++;
return cnt3;
}
The other thing is you're passing file to it (which is a string) instead of ifs. Change the call to:
a = num_of_lines( ifs );

How do I print the content of a file?

How can I print the contents of a file, the name of which is specified via my program's command line?
I do not know how to give the name of file by command line and how to work with it.
For ex this is does not work:
int main(int argc, char *argv[])
{
FILE *f;
char s[20];
cin >> s;
f=fopen_s(s,"rt");
std::cout << f;
_getch();
return 0;
}
error C2660
#include <iostream>
#include <fstream>
int main(int argc , char *argv[])
{
if(argc < 2)
{
std::cout << " Wrong usage " << std::endl;
exit(0);
}
std::string file_name = argv[1];
std::ifstream fs;
fs.open(file_name.c_str());
std::cout << file_name << std::endl;
std::string line ;
while(fs >> line)
{
std::cout << line << std::endl;
}
return 0;
}
You cannot use << operator with char[]
Solution : You can use std::string
std::string s;
Use the string's c_str() value as name in fopen_s(name, "rt")
Solution : You need to put the file in the same directory as the executable
f = fopen_s(s.c_str(), "rt");
You cannot cout << FILE *f
Solution : read file content line by line as you print each line
char* line; //used to receive data for each line
int length; //used to represent how many characters have received
while ((getline(&line, &length, f) != -1) {
print("%s", line);
}

Pipes boost::iostreams don't have any output

I'm trying using pipes with boost libraries, I just want to execute a background program(e.g.: ls) and get it's output in a string(like you can do with fopen and fread), but I really can't get why I have no output with this code:
#include <iostream>
#include <cstdio>
#include <sstream>
#include <boost/iostreams/stream.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
int
main(int argc, char** argv)
{
using namespace boost::iostreams;
if(argc < 2) {
return -1;
}
FILE* p = popen(argv[1], "r");
if(! p) {
std::cerr << "error open pipe" << std::endl;
return -2;
}
int fd = fileno(p);
std::stringstream ss;
ss << fd;
std::string s = ss.str();
file_descriptor_source pdesc(s);
stream_buffer<file_descriptor_source> pstream(pdesc);
std::istream is(&pstream);
std::string out;
while(is) {
std::getline(is, out);
std::cout << out << std::endl;
}
pstream.close();
pdesc.close();
pclose(p);
return 0;
}
Thanks in advance.
It seems you are trying to open a boost::file_descriptor_source from a "path" which contains the file descriptor number. However, a file of this name probably doesn't exist. What you probably meant to use is something like this:
if (FILE* p = popen(argv[1], "r"))
{
boost::iostreams::file_descriptor_source d(fileno(p), boost::iostreams::close_handle);
boost::iostreams::stream_buffer<boost::iostreams::file_descriptor_source> pstream(d);
std::cout << &pstream;
pclose(p);
}