T.R | Title | User | Personal Name | Date | Lines |
---|
3489.1 | | TLE::D_SMITH | Duane Smith -- DEC C RTL | Thu Mar 13 1997 06:41 | 4 |
| In C, the functions are called "truncate" and "ftruncate". They were
added in OpenVMS V7.
Duane
|
3489.2 | that's good; how to use them? | CUJO::SAMPSON | | Thu Mar 13 1997 20:36 | 1 |
| Oh! Can they be applied to an open fstream object? If so, how?
|
3489.3 | answer to .2 | DECC::J_WARD | | Fri Mar 14 1997 12:50 | 4 |
|
You can always get access to the underlying C file
descriptor for an fstream by calling rdbuf()->fd().
Then use the file descriptor for the ftruncate() call.
|
3489.4 | oops, I was thinking trunc-at-x, not trunc-here | SPECXN::DERAMO | Dan D'Eramo | Fri Mar 14 1997 15:42 | 50 |
| // for discussion purposes only ...
#include <iostream.hxx>
#include <fstream.hxx>
#include <unistd.h>
class Trunc { // Sample usage: ofstrm << Trunc(100);
public:
int offset;
Trunc() :offset(0) {}
Trunc(int o) :offset(o) {}
};
ofstream &operator<<(ofstream &ofs, const Trunc &t)
{
filebuf *fbp = ofs.rdbuf();
if (fbp == 0) { /* error handling ... */ }
int fd = fbp->fd();
if (fd < 0) { /* error handling ... */ }
// anything else you need/want to do first, such as
// flushing the buffer ...
int result = ftruncate(fd, t.offset);
if (result != 0) { /* error handling ... */ }
// put the ofstream and filebuf objects back into a
// consistent state with respect to the now truncated
// file ...
// Must return the ofstream argument.
return ofs;
}
Three problems ... First is the missing code fragments.
Second, as is a Trunc object can't encode "truncate at current
position", it can only encode an offset. Third, Trunc objects
don't play well with cascading <<'s. For example,
const char done[] = "\nDone\n";
ofstrm << done << Trunc(currpos + strlen(done));
This is (ofstrm << done) << Trunc(...) and the type of the
left hand side argument is ostream, not ofstream, but the only
defined << with a Trunc on the right hand side takes an
ofstream on the left hand side.
Dan
|
3489.5 | find current position with ftell()? | CUJO::SAMPSON | | Fri Mar 14 1997 21:45 | 2 |
| Okay, cool, thanks! So, the answer is: the needed tools are
available in OpenVMS V7.0; after that, it's just a SMOP...
|
3489.6 | ftruncate() opens and closes the file | DECC::J_WARD | | Mon Mar 17 1997 09:10 | 10 |
|
From the ftruncate() man page:
path Specifies the name of a file that is opened, truncated, and then
closed. The path parameter must point to a pathname which names
...
So if what the user wants is to be able to truncate the file
without also opening and closing it, then I don't think this command
will help.
|
3489.7 | | SPECXN::DERAMO | Dan D'Eramo | Mon Mar 17 1997 10:48 | 40 |
| But the path argument is passed to truncate(). ftruncate()
takes a file descriptor to an already opened file.
How much information does an ofstream and/or filebuf object
cache about the state of the file that will be messed up by
calling ftruncate() with the file descriptor?
Dan
truncate(2) truncate(2)
NAME
truncate, ftruncate - Changes file length
SYNOPSIS
#include <sys/types.h>
int truncate (
const char *path,
off_t length );
int ftruncate (
int filedes,
off_t length );
PARAMETERS
path Specifies the name of a file that is opened, truncated, and then
closed. The path parameter must point to a pathname which names
a regular file for which the calling process has write permis-
sion. If the path parameter refers to a symbolic link, the
length of the file pointed to by the symbolic link is truncated.
filedes Specifies the descriptor of a file that must be open for writing.
length Specifies the new length of the file in bytes.
|
3489.8 | might work for this customer | CUJO::SAMPSON | | Mon Mar 17 1997 21:08 | 7 |
| > How much information does an ofstream and/or filebuf object
> cache about the state of the file that will be messed up by
> calling ftruncate() with the file descriptor?
Hmmm. Would it work out okay if the file were closed
immediately after the ftruncate() call? Perhaps by
next calling fclose(), then close on the fstream?
|