Partially Truncating a Stream (Fstream or Ofstream) in C++

Partially truncating a stream (fstream or ofstream) in C++

I don't think you can. There are many functions for moving "up and down" the wrapper hierarchy for HANDLE<->int<->FILE *, at least on Windows, but there is no "proper" to extract the FILE * from an iostreams object (if indeed it is even implemented with one).

You may find this question to be of assistance.

Personally I would strongly recommend steering clear of iostreams, they're poorly designed, heavily C++, and nasty to look at. Take a look at Boost's iostreams, or wrap stdio.h if you need to use classes.

The relevant function for stdio is ftruncate().

fstream - shrinking file

File streams don't support truncation except when being opened. You'll need to close it and then use an OS specific method to set the precise length. For Windows and Linux that is:

Windows: SetEndOfFile()

Linux: truncate()/ftruncate()

How to overwrite only part of a file in c++

If the replacement string is the same length, you can make the change in place. If the replacement string is shorter, you may be able to pad it with zero-width spaces or similar to make it the same number of bytes, and make the change in place. If the replacement string is longer, there just isn't enough room unless you first move all remaining data.

How can I derive my own stream from a standard stream?

I think there are three levels of answer to this question:

Level 1: It is complicated, especially if you are completely new to C++, stop right now. Only if you feel adventurous, continue to level 2.

Level 2: Use some library that makes creating streams easier. I would suggest using Boost.IOStreams library. It makes creating own streams and streambufs much easier. If you are still not satisfied, continue to level 3.

Level 3: You will have to derive from std::streambuf and modify its behaviour to suit your needs. Then you will have to plug your streambuf into own stream.

Is opening the SAME file in two different fstreams Undefined Behaviour?

basic_filebuf::open (and all things that depend on it, like fstream::open) has no statement about what will happen in this case. A filesystem may allow it or it may not.

What the standard says is that, if the file successfully opens, then you can play with it in accord with the interface. And if it doesn't successfully open, then there will be an error. That is, the standard allows a filesystem to permit it or forbid it, but it doesn't say which must happen. The implementation can even randomly forbid it. Or forbid you from opening any files in any way. All are (theoretically) valid.

Does C++ ofstream file writing use a buffer?

Yes, ostreams use a stream buffer, some subclass of an instantiation of the template basic_streambuf. The interface of basic_streambuf is designed so that an implementation can do buffering if there's an advantage in that.

However this is a quality of implementation issue. Implementations are not required to do this but any competent implementation will.

You can read all about it in chapter 27 of the ISO standard, though maybe a more readable source is The C++ Standard Library: A Tutorial and Reference (google search).



Related Topics



Leave a reply



Submit