473,403 Members | 2,284 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

How to append context to a FILE?

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

Jul 23 '05 #1
12 5312
> 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::ofstream ofs("file", std::ios_base::out | std::ios_base::app);

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

Jonathan

Jul 23 '05 #2
Jonathan Mcdougall wrote:
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::ofstream ofs("file", std::ios_base::out | std::ios_base::app);

if (ofs)
{

ofs << "This is appended or not!"; }
}


Jul 23 '05 #3
Abecedarian wrote:
Jonathan Mcdougall wrote:
> 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::ofstream ofs("file", std::ios_base::out | std::ios_base::app);

if (ofs)
{

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::openmode],
Table 86 -- openmode effects:

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

Best

Kai-Uwe Bux
Jul 23 '05 #4
Kai-Uwe Bux wrote:
Abecedarian wrote:
Jonathan Mcdougall wrote:
# include <fstream>

int main()
{
std::ofstream ofs("file", std::ios_base::out | std::ios_base::app);
if (ofs)
{ 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::openmode], 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::

Jul 23 '05 #5
>> 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::openmode],
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

Jul 23 '05 #6
Jonathan Mcdougall wrote:
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.

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

::A::

Jul 23 '05 #7
>>>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
... 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

Jul 23 '05 #8
Jonathan Mcdougall wrote:
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::openmode],
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.


Actually, there is. operator<< can set failbit just like operator>>.
A possible reason for failure is the filesystem being full.
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

Jul 23 '05 #9
Abecedarian wrote:
Jonathan Mcdougall wrote:
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.

... 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

Jul 23 '05 #10
Kristo wrote:
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::

Jul 23 '05 #11
Abecedarian wrote:
Kristo wrote:
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

--
Anti-spam address, change each 'X' to '.' to reply directly.
Jul 23 '05 #12
>> close will set failbit if it fails.

For whatever reason.

std::ofstream 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.
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.


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.

--
Jonathan
[FAQ] - http://www.parashift.com/c++-faq-lite/

Jul 23 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: tertius | last post by:
Is there a better way to append certain chars in a string with a backslash that the example below? chr = "#$%^&_{}" # special chars to look out for str = "123 45^ & 00 0_" # string to...
7
by: fremenusul | last post by:
I know I have been asking LOTS of xml questions and I really apprecaite all the help. Here is my XML file <?xml version="1.0" encoding="utf-8" ?> <products> <!-- Repeat the structure below...
2
by: fremenusul | last post by:
I orginally asked this question on friday, and made some progress, but now I am getting an error. What is the type of xmlProductName (check the runtime type in the debugger)? The debug output...
3
by: Jonathan Buckland | last post by:
Can someone give me an example how to append data without having to load the complete XML file. Is this possible? Jonathan
2
by: Matrix | last post by:
Greetings I want to append records in a XML file,I use XMLTextWriter,but i found it only create a XML file,not append records in exist XML file.why?...
4
by: Amy L. | last post by:
It is my understanding that in order to append XML to a file you would have to read the file in as an XML document and insert the node. However, what if the file is close to 1GB in size? Would you...
5
by: Simone M | last post by:
Here is my problem: I need to append a string to a file every time the user downloads this file. The string is different for every user. I would like to append the string to the file via ftp. But...
2
by: Gaby Sandoval | last post by:
I have this code. The user can read teh record from the text file just fine. They can click the NEXT button and it reads the next record (which is just the next line from the text file). If the...
3
by: Meendar | last post by:
Hi, I just want to append the arguments from the function in javascript to the variables as function a() { var temp=arguments;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.