 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
lotusny78@yahoo.com Guest
|
Posted: Wed Oct 12, 2005 12:58 am Post subject: why does ofstream(..., ate) file |
|
|
When using VC++ .Net, the statement:
ofstream myFile(orderFileName.c_str(), ios_base::ate);
causes the file to be truncated.
1) Is this a bug or am I going crazy?
2) What's the point of seeking to the end of the file if you're just
going to truncate it?
3) Also, if you open with ios_base::app, can you move the put pointer
to the middle of the file and write there later on?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Oct 12, 2005 8:50 am Post subject: Re: why does ofstream(..., ate) file |
|
|
[email]lotusny78 (AT) yahoo (DOT) com[/email] wrote:
| Quote: | When using VC++ .Net, the statement:
ofstream myFile(orderFileName.c_str(), ios_base::ate); causes
the file to be truncated.
1) Is this a bug or am I going crazy?
|
Your just mixing issues:-). The ios::ate flag has no effect on
how the file is initially opened. All it does is request a seek
to the end immediately after opening.
| Quote: | 2) What's the point of seeking to the end of the file if
you're just going to truncate it?
|
Good question. ios::ate is pretty useless when opening a file
for writing. It's also pretty useless when opening a file for
reading, since it means that the first read will automatically
return end of file. And if you're doing both reading and
writing, you'll be doing your own seeking anyway, so what's the
point?
| Quote: | 3) Also, if you open with ios_base::app, can you move the put
pointer to the middle of the file and write there later on?
|
No. The point of ios::app is precisely that every write will be
to the end of the file. The intent is for this to be atomic, if
the system supports it. Something you can't do otherwise.
I'm not sure what you're trying to do. As Plauger recently
pointed out in response to another question (involving mixing
peek() and seek() on the same stream), iostreams supports two
models: a streams model, and a random access, and the two don't
work well together. (My experience has been that the random
access model is sort of a hack anyway, and only works on binary
streams.)
With regards to open: ios::app applies to the streaming model,
and only when writing; one of the goals is to allow correct
streaming when several different processes are writing to the
same file. (Whether it is really appropriate with the iostream
formatting model is another question. It really only works if
you format your data into a string, and output that in a single
operation.)
Personally, I don't quite see what the use of ios::ate is. But
the semantics are defined in terms of seek, so I would presume
that it is only relevant for the random access mode. And as far
as I can tell, the random access mode only really works well
files are opened for bidirectional access (read and write), and
in binary mode. (There are some things you can do in text mode,
but they are limited by the fact that in text mode, the
parameter to seek must be a value previously returned by tell,
unless it is to the beginning or the end of the file.) So if
random access is desired, you should open the file with:
ofstream myFile( orderFileName.c_str(),
ios::in | ios::out | ios::binary ) ;
(Strictly speaking, the ios::out isn't necessary, since the open
function in ofstream will automatically or it in. I just find
it a bit clearer -- otherwise, the presence of ios::in suggests
only input.)
If you do this, you should be able to add ios::ate to the flags
as well, without any problem. Or you could just seek to the end
immediately after the open.
Note that some systems do guarantee more than the standard;
under Unix, for example, ios::binary is actually a no-op, and
isn't necessary for seeking. Under Windows, if you know where
the newlines are in the file, and count each newline for two
bytes, you can also seek to arbitrary positions in text files.
But these are system specific extensions, and not guaranteed by
the standard.
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
anas.hashmi@gmail.com Guest
|
Posted: Sun Oct 16, 2005 9:54 am Post subject: Re: why does ofstream(..., ate) file |
|
|
I am trying to write to the beginning of a file.
The reason:
I want to make a form where webmasters can use it to insert in updates
to a webpage without having to go directly into the file and start
editing. The form must write to the beginning of file so that the
soonest update appears first.
I am actually writing in PHP but see that they do not support it. So I
turned to C++. On About.com, the tutorial on C++ Lesson 12 says that
ios::ate is used to write anywhere in the file without truncating. So
i did a search on how I can manipulate that to the beginning of a file.
I could not find anything.
Is there anyone who can help solve this problem?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Valentin Samko Guest
|
Posted: Sun Oct 16, 2005 3:49 pm Post subject: Re: why does ofstream(..., ate) file |
|
|
[email]anas.hashmi (AT) gmail (DOT) com[/email] wrote:
| Quote: | I am trying to write to the beginning of a file.
The reason:
I want to make a form where webmasters can use it to insert in updates
to a webpage without having to go directly into the file and start
editing. The form must write to the beginning of file so that the
soonest update appears first.
I am actually writing in PHP but see that they do not support it. So I
turned to C++. On About.com, the tutorial on C++ Lesson 12 says that
ios::ate is used to write anywhere in the file without truncating. So
i did a search on how I can manipulate that to the beginning of a file.
I could not find anything.
Is there anyone who can help solve this problem?
|
You can not write to the beginning of a file without overwriting
existing data, i.e. you can not "insert" data anywhere but at the end.
This has nothing to do with C++ or PHP. This is how file systems work.
--
Valentin Samko - http://val.samko.info
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Oct 16, 2005 4:16 pm Post subject: Re: why does ofstream(..., ate) file |
|
|
[email]anas.hashmi (AT) gmail (DOT) com[/email] wrote:
| Quote: | I am trying to write to the beginning of a file.
The reason:
I want to make a form where webmasters can use it to insert in
updates to a webpage without having to go directly into the
file and start editing. The form must write to the beginning
of file so that the soonest update appears first.
I am actually writing in PHP but see that they do not support
it. So I turned to C++. On About.com, the tutorial on C++
Lesson 12 says that ios::ate is used to write anywhere in the
file without truncating. So i did a search on how I can
manipulate that to the beginning of a file. I could not find
anything.
|
It sounds to me like you are trying to insert at the beginning
of the file. This isn't possible in C++, for the simple reason
that it isn't possible under many (most?) operating systems; if
you position at the beginning of the file and write, you will
overwrite what was previously there.
The only way to work around this restriction is to copy the
file. Something along the lines of:
create temporary file
write new data to temporary file
copy existing file to temporary file
delete existing file
rename temporary file
I won't go into the details here, because they shouldn't be a
problem for an experienced C++ programmer, and because you say
you were working in PHP, and I would be very surprised if you
cannot do the same thing in PHP.
Since the solutions do abandon C++ entirely, I might as well go
all the way: if you store the Web page data as SQL records, it
should be possible to insert the new data without copying the
old. Just associate a timestamp with each blob, and retrieve
sorted by timestamps.
--
James Kanze mailto: [email]james.kanze (AT) free (DOT) fr[/email]
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
anas.hashmi@gmail.com Guest
|
Posted: Tue Oct 18, 2005 10:28 am Post subject: Re: why does ofstream(..., ate) file |
|
|
Thanks.
create temporary file
write new data to temporary file
copy existing file to temporary file
delete existing file
rename temporary file
the suggestion here is what i ended up doing instead. or kind of.
at first, i thought by deleting existing file, i would make the file
unavailable... but all this happens in a matter of nanoseconds so it
was not a problem.
Thanks a whole lot everyone.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze Guest
|
Posted: Wed Oct 19, 2005 8:50 am Post subject: Re: why does ofstream(..., ate) file |
|
|
[email]anas.hashmi (AT) gmail (DOT) com[/email] wrote:
| Quote: | Thanks.
create temporary file
write new data to temporary file
copy existing file to temporary file
delete existing file
rename temporary file
the suggestion here is what i ended up doing instead. or kind
of.
at first, i thought by deleting existing file, i would make
the file unavailable... but all this happens in a matter of
nanoseconds so it was not a problem.
|
It could be a problem if your system crashed precisely in those
nanoseconds. It's a fairly common practice when using this
technique to use recognizable names for the temporary files, and
for the application to look for them on start-up, and initiate
recovery procedures. An important point concerning the above
sequence is that there is never a moment when neither the new
nor the old version are on disk, so there is never a moment when
you cannot recover one of the two. (Note that this is also an
argument against in place modification, even when possible.
With in place modification, there may be times when the only
copy on disk is in an incoherent state.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|