Empty file check on std::ofstream - c++

I've been using std::ofstream for writing purposes quite a bit. I open the file, do some operations based on certain conditions and close it.
Let's say that later I want to check if anything is really written into the file or not. There is not is_emtpy() kind of simple check available with std::ofstream.
One way I thought is of using the stat way which is independent of std::ofstream.
Wonder how do everyone else do it?

Standard output streams provide a tellp() method which can be used to determine the current write position. The type and meaning of the return value is implementation-defined, but for it to be useful, it must return distinct values.
#include <iostream>
#include <fstream>
int main()
{
std::ofstream out{"/tmp/test"};
auto const empty_pos = out.tellp();
std::clog.setf(std::ios_base::boolalpha);
std::clog << "At start: " << (out.tellp() != empty_pos) << '\n';
out << 'c';
std::clog << "After writing 1 char: " << (out.tellp() != empty_pos) << '\n';
}
In principle, empty_pos may be different for each stream, so a truly portable program will take that into account.
Note also that this doesn't necessarily mean that the output is visible to other programs - use std::flush if that's important.

Related

Which is the best way to print to the console in c++?

I have read three ways to print things to the console in c++ from various sources.
Using using namespace std; and then using cout (CodeBlocks Standard)
Not using the above and using std::cout and std::endl; (C++ Primer)
Using printf (HackerRank)
Which is preferred and why?
Number 2 with amendment. (std::cout and '\n')
Why?
Because you should avoid using namespace std. Source
(Among other reasons) Because cout is typesafe and printf is not. Source
std::endl will force a flush of the output buffer to the console. Unless you specifically want this to happen use << '\n' or << "...string\n". Source
Unless you really care about speed, both cout and printf are fine. If you want faster runtimes, here are a few pointers :
Use only printf with no cout. This will give more speed than using a mixture of printf and cout or just cout.
Or use only cout but add the following at the beginning of execution
ios_base::sync_with_stdio(false);cin.tie(NULL); . There are two separate streams for printf and cout and they are synchronized by default. Lot of running time is wasted due to this synchronisation. These two lines of code will stop the synchronisation, but take care that you don't use any printf if you add these lines, otherwise printing might happen in random order.
Do not use endl unless you want to flush the output buffer. Lots of endl can make the code slower. Use cout<<'\n'; instead.
these is my debugger code that during these 10 years of c++ working helped me.
std::ostream &debugRecord (const char* fileName, int lineNum, const char* funcName)
{
std::lock_guard<std::mutex> lock(streamMutex_);
return std::cout << "Thread # " << getCurrentThreadId() << " -- "
<< "(" << fileName << ":" << lineNum << "): " << funcName << std::endl;
}
Both your first points do basically the same thing. It's better practice to use std:: instead of using namespace std; as the latter pollutes the global namespace and can cause naming conflicts.
Something not mentioned is that you can selectively expose parts of a namespace with using <namespace>::<element>; (e.g. using std::cout;). It's still better practice to be verbose with your statements, but this option still isn't as bad as exposing the entire namespace.
printf isn't as safe as cout (the stream << operators do a good job of printing what you want), you ought to avoid it while starting out.
The answer depends a lot on what you want to do. For output which largely uses default formats cout is indeed preferred because of the type safety and because it's very intuitive.
If you want to heavily format your output though I can only recommend the surprisingly versatile and straight-forward printf because manipulators in cout are a pain. True: The printf format syntax, does, let's say, take some getting used to, but it's surely worth it. Just double check the format string, listen to the warnings of your compiler, and use the proper format specifiers e.g. for size_t and other system dependent data in order to stay portable.
There is also a boost facility for combining streams and printf style formatting, see https://stackoverflow.com/a/15106194/3150802, but I have never used it. Perhaps somebody can comment on its usability?

iomanip / fixed width persistence

Why does
#include <iostream>
#include <iomanip>
int main()
{
std::cout << std::fixed << std::setw(4) << std::setprecision(0);
std::cout << 4;
std::cout << 4;
}
print
" 44
(ignore the quote, it's just to get the formatting right)
and not
" 4 4
?
I thought that the iostream 'modifiers' persistent on the stream until they are explicitly changed/reset. I have a bunch of numbers I need to print with a certain prefix so that all fields have equal width; should I re-apply my modifiers every time I print one? Doesn't seem very efficient.
Unfortunately you've wandered into one of the areas of the standard that's a little archaic and seemingly without any overarching design goals.
This is undoubtedly historic as the iostreams library AFAIAA, was not originally part of the STL which is what became the standard library.
It's worth reading the notes on all std::ios_base members and the associated manipulators.
For example:
http://en.cppreference.com/w/cpp/io/ios_base/width
Some I/O functions call width(0) before returning, see std::setw (this results in this field having effect on the next I/O function only, and not on any subsequent I/O)
The exact effects this modifier has on the input and output vary between the individual I/O functions and are described at each operator<< and operator>> overload page individually.
Anticipating:
But that's just <insert expletive>!!!
A: yup.

What is the purpose of storing the content in memory before printing it?

I am new to C++ and currently using visual studio.
I see on many tutorials online that when reading from a file with ifstream, before we cout the contents when looping through each line, we write the contents of the current line to a char data[] var then cout the data.
Something like the following
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream f("D:\\file1.txt");
char a[80];
while(!f.eof())
{
f >> a;
cout << a << endl;
}
return(0);
}
What is the point of
char a[80];
...
f >> a;
cout << a << endl;
When we could just do
cout << f << endl;
And save declaring a char and wasting more lines of code?
The preferred method in C++ is:
#include<iostream>
#include<fstream>
int main()
{
std::ifstream f("D:\\file1.txt");
if( f )
{
std::string line;
while(getline(f, line) )
{
std::cout << line << endl;
}
}
return 0 ;
}
If you want to copy, or list files, use operating system commands. The operating system commands are more optimized for handling files. Also, they already exist and have been tested so you don't waste your time.
What is the purpose of storing the content in memory before printing it?
In your example, there isn't much of a point. The data being read from std::cin is being sent directly to std::cout to be displayed on the console. Generally, the only reasons you'd want to store the data in the program memory before printing it is if you want to modify the data or check properties of the data and take certain actions based on those properties.
It should be noted that while this is a common example, the use of while (!eof()) is the incorrect way to check the validity of the stream before reading data. This method checks the stream before the input is read, which can lead to undefined behavior if invalid data is read and subsequently used. The normal way to read data is to check the validity of the stream after performing the read. For example, in your program this would be changed to:
while (f >> a)
{
std::cout << a << std::endl;
}
After the read is performed, the stream will be converted to a boolean. It will return true or false depending on the validity of the stream. If the stream read the end-of-file (EOF) character then that would be a failed read and the stream will return false.
What is the point of
char a[80];
...
f >> a;
cout << a << endl;
when we could just do
cout << f << endl;
First, cout << f will not do what you expect. The stream insertion operator (operator<<()) is overloaded for certain types. f is of type std::ifstream - a type for which this operator is not overloaded. Pre-C++11 C++ IOStreams contained a conversion to void* so that they could be used in boolean contexts. The stream insertion operator is overloaded for pointers to void, so the output you would get is not something you'd expect. As of C++11 you'd get a compiler error that no operator overload could be found for that type.
There is, however, an overload for std::streambuf*, a pointer an IOStreams buffer. Each stream has a buffer that stores and maintains characters from the source or sink. The overload for this operator reads data from the buffer and sends it to its own buffer, so you can do something like this:
std::cout << f.rdbuf();
rdbuf() returns a pointer to the stream's buffer.
While this is an effective use of the stream's capabilities, the data is still being stored in the buffer of std::cout. Streams are buffered and data sent into the source or sink are consigned to a buffer where it waits until the buffer is flushed. You can use std::nounitbuf to unbuffer std::cout in order to write directly to the external device:
std::cout << std::nounitbuf
<< f.rdbuf();
For a simple example with a small file, buffering really isn't needed. If you have a very large file then buffering is very useful as the program doesn't have to make a system call for each character being inserted.

What is the C++ idiom to replace snprintf(3)?

I have some C++ code which needs to generate an error message when parsing a certain file header fails. In this case, I need to ensure that a certain 4 byte field in the header is "OggS", and if it is not, return an error message like "invalid capture_pattern: 'FooB'; expecting 'OggS'". My code looks something like this:
const string OggPage::parseHeader(void) {
read(fd, capture_pattern, sizeof(capture_pattern)); // error handling omitted
if (strncmp(capture_pattern, CAPTURE_PATTERN, sizeof(capture_pattern)) != 0) {
char err[256];
snprintf(err, sizeof(err), "Failed to read %d bytes from file descriptor %d: %s\n", sizeof(capture_pattern), fd, err);
return err;
}
return "Everything was A-OK!";
}
What is the standard C++ idiom for building a string from other datatypes? I'm not wedded to the printf(3)-style format here, so feel free to suggest anything that works.
You can use stringstream or ostringstreamfrom C++ Standard library. If further stream will not be used to read values from it (e.g. it's istream part won't be used) ostringstream is more suitable.
#include <sstream>
#include <string>
#include <iostream>
int main() {
std::stringstream str_stream;
int number = 5;
str_stream << " String with number " << number << std::endl;;
std::string str = str_stream.str();
std::cout << str;
}
Just as a note, please don't suggest replacements for the actual reading--there is a reason that I want to use the C standard library for I/O here. :)
You can't really ask for "idiomatic" ways to do something in C++, and then say "but I want to stick to the C standard library...
Anyway, you have three options. I don't believe any of them is idiomatic (because that would imply some kind of consensus about this being the best way):
stick with the C APIs you're already using
Use the the C++ std::stringstream, which is part the iostreams section of the standard library
use Boost.Format
The latter has the downside that it relies on a third-party library, but the advantage that it gives you printf-like syntax, in a typesafe and extensible manner, which interoperates cleanly with C++ streams.
Using string functions from cstring is definitely fine for reading. They're fast, convenient and not verbose.
For building your error messages, you can use stringstream, operators related to the string class, boost::format or, as you put it, snprintf.
You have also boost::lexical_cast for simple things:
string message = "Failed to read " + lexical_cast<string>(n) +
" bytes from the descriptor " + lexical_cast<string>(fd) + ".";
I recommend having a look at this Gotw for a sane point of view.
I'd recommend boost::format if you have a lot of formatting to do. Use lexical_cast for simple isolated things, and use stringstream if you have requirements which makes you need them (eg. custom operator<<, cannot use boost, etc).
To be honest, snprintf is really fine.
C++ way of doing this is std::stringstream.
std::stringstream err;
err << "Failed to read " << sizeof(capture_pattern)
<< " bytes from fd " << fd << std::endl;
return err.str();
You create a stringstream, write what you want using the '<<' operator and then get the string out of stream with the member function .str().
#include <sstream>
#include <string>
using namespace std;
stringstream err;
err << "Failed to read " << sizeof(capture_pattern) << " bytes from file descriptor: " << fd << endl;
string outstr = err.str();

Transparently manipulating strings inserted into an ostream

I'd like to provide an std::ostream that may or may not, from the user's point of view, encrypt its contents.
Imagine some random function that uses an std::ostream&:
void write_stuff( std::ostream& stream ) {
os << "stuff";
}
Whether stuff is output in cleartext or is encrypted is dependent on how the stream argument was initialized.
I'm thinking about inheriting from std::basic_streambuf, but I'm not sure it's the best way to go. I think the ideal scenario is some sort of filter object which is run before the contents are actually output. That way it'd be easy to chain operations, e.g. encrypting and then hex-encoding, which is a benefit that making my own basic_streambuf does not seem to give (not easily at least).
Any advices?
Thanks.
UPDATE: I followed the strategy laid out by the accepted answer. I found this article on writing a custom streambuf which performs XOR obfuscation to be extremely helpful as a starting point.
A streambuf can be implemented in terms of another arbitrary streambuf that's either passed in as an argument to the constructor or set with a special member function. And that allows you to stack streambuf implementations like you were thinking of.
You could even make a manipulator that uses the ostream's rdbuf function to get the current streambuf, call a function on your streambuf that sets the 'stacked' streambuf, and then calls rdbuf again to replace the ostream's streambuf with your own.
aes_ctr_streambuf encrypter(key);
zlib_streambuf compressor;
::std::cout << stack_streambuf(encrypter) << stack_streambuf(compressor);
It is hard to describe in short what you have to do in order to create an I/O stream for the new source or sink. Luckily, Jonathan Turkanis and CodeRage, LLC have created very nice building blocks with exceptional documentation that can help you save hundreds of hours of research and development of new streams. The library is called Boost.Iostreams.
The second example in documentation exactly shows how to solve your problem.
Also, note that they already have an output stream with zip compression. So maybe you don't even need to write a single line of code.
Good luck!
I'm not sure that what I'm about to suggest is exactly what you need (and its too complicated to be a comment), but have you ever heard of the stream manipulators ?
#include <iostream>
#include <iomanip>
int main(int, char**)
{
int a = 10;
std::cout << "a: " << a << std::endl; //outputs: "a: 10"
std::cout << "a: " << std::hex << a << std::endl; //outputs: "a: a"
std::cout << "a: " << std::dec << a << std::endl; //outputs: "a: 10"
return EXIT_SUCCESS;
}
You may indeed use a similar pattern (this matches what you actually call a "filter" object) to somehow change the state of your custom stream object.