How to append context to a FILE?

Z

zhushenli

Hello all,

In c,i can open a file with argv append, How to append context to a
FILE in c++ fstream? Thanks!

Regards,
Davy
 
J

Jonathan Mcdougall

In c,i can open a file with argv append,
How to append context to a FILE in c++
fstream? Thanks!

# include <fstream>

int main()
{
std::eek:fstream ofs("file", std::ios_base::eek:ut | std::ios_base::app);

if (ofs)
{
ofs << "This is appended";
}
}

Jonathan
 
A

Abecedarian

Jonathan said:
# include <fstream>

int main()
{
std::eek:fstream ofs("file", std::ios_base::eek:ut | std::ios_base::app);

if (ofs)
{
ofs << "This is appended or not!";
 
K

Kai-Uwe Bux

Abecedarian said:
ofs << "This is appended or not!";

If you have doubts about the validity of the posted solution, I would
appreciate a more detailed explanation since I think, the posted solution
is correct. From the c++ standard, clause 27.4.2.1.4 [lib.ios::eek:penmode],
Table 86 -- openmode effects:

app: seek to end before each write
out: open for output

Best

Kai-Uwe Bux
 
A

Abecedarian

Kai-Uwe Bux said:
If you have doubts about the validity of the posted solution, I would
appreciate a more detailed explanation since I think, the posted solution
is correct. From the c++ standard, clause 27.4.2.1.4 [lib.ios::eek:penmode],
Table 86 -- openmode effects:

app: seek to end before each write
out: open for output

And where is checked whether
ofs << "This is appended or not!";
succeeds?

But I agree with you. Most textbooks and most newsgroup postings would
recommend a similar solution. So let's pretend it's correct.

::A::
 
J

Jonathan Mcdougall

If you have doubts about the validity of the posted solution, I
would
appreciate a more detailed explanation since I think, the posted
solution
is correct. From the c++ standard, clause 27.4.2.1.4
[lib.ios::eek:penmode],
Table 86 -- openmode effects:
app: seek to end before each write
out: open for output
And where is checked whether
ofs << "This is appended or not!";
succeeds?

There is no way to do that in standard C++, except by reading in memory
the content of the file before and after and comparing these versions.
But I agree with you. Most textbooks and most newsgroup postings would
recommend a similar solution. So let's pretend it's correct.

I'll confess I don't understand your point here. First, you say, in an
obscure way, that I'm wrong, and then you "agree" and say "let's
pretend it's correct". What exactly do you mean?


Jonathan
 
A

Abecedarian

Jonathan said:
There is no way to do that in standard C++, except by reading in memory
the content of the file before and after and comparing these
versions.

.... or by using the ofstream.close() function. I have no idea though
how to check the success of ofstream.close().

::A::
 
J

Jonathan Mcdougall

And where is checked whether
... or by using the ofstream.close()
function.

And what would that do, except closing the
stream?
I have no idea though
how to check the success of
ofstream.close().

Then why do you mention it? Why are you
responding to questions you don't
understand with answers you can't even
explain?

Jonathan
 
K

Kristo

Jonathan said:
If you have doubts about the validity of the posted solution, I would
appreciate a more detailed explanation since I think, the posted
solution
is correct. From the c++ standard, clause 27.4.2.1.4
[lib.ios::eek:penmode],
Table 86 -- openmode effects:
app: seek to end before each write
out: open for output
And where is checked whether
ofs << "This is appended or not!";
succeeds?

There is no way to do that in standard C++, except by reading in
memory the content of the file before and after and comparing
these versions.
But I agree with you. Most textbooks and most newsgroup postings would
recommend a similar solution. So let's pretend it's correct.

I'll confess I don't understand your point here. First, you say, in
an obscure way, that I'm wrong, and then you "agree" and say "let's
pretend it's correct". What exactly do you mean?

While it's possible for operator<< to fail, it doesn't happen very
often. In most cases, you just output something to a file and forget
about it. If one write fails, all subsequent writes fail, but it won't
hurt your program. IMHO, if a call to operator<< ever fails on me, I
have bigger problems than my output not being complete.

Kristo
 
K

Kristo

Abecedarian said:
versions.

... or by using the ofstream.close() function. I have no idea though
how to check the success of ofstream.close().

close will set failbit if it fails. But, you're done with the file
anyway so it doesn't matter.

Kristo
 
A

Abecedarian

Kristo said:
close will set failbit if it fails.

I thought of something like that. But I never use iostreams when I have
to write important data to a file.
But, you're done with the file anyway so it doesn't matter.

I may not matter to you. It matters to the user of your program who
loses many hours of work.

::A::
 
L

Larry I Smith

Abecedarian said:
Kristo said:
close will set failbit if it fails.

I thought of something like that. But I never use iostreams when I have
to write important data to a file.
[snip]
::A::

Why? Have you encountered problems? If so, with which OS,
compiler, lib? What problems (so I can avoid them)?

What do you use?
read/write?
fread/fwrite?
fprintf/fscanf?

Regards,
Larry
 
J

Jonathan Mcdougall

close will set failbit if it fails.

For whatever reason.

std::eek:fstream ofs;
ofs.close();

sets the failbit. But I fail to see what this
brings to the discussion on how to append to files?
I thought of something like that. But I never use
iostreams when I have to write important data to
a file.

Oh.. I wouldn't have noticed.
doesn't matter.
I may not matter to you. It matters to the user
of your program who loses many hours of work.

iostreams are not more "dangerous" than any other
means of accessing files, if used correctly. C++
iostreams are usually implemented using either C
functions or os dependent function, so it really
does not matter in the end.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top