File writing with C++

A

Admin

I have a program that needs to have a couple things happen.

First, the file needs to constantly be updated with new information.
Because the function in which I do this is not always running, I need
to be able to close the file and re-open it.

I know this can be accomplished (in theory) by the following, but for
some reason, I don't have any luck with it. I can't get the program
to open the file, write to it, close it, then re-open it for more file
writing.

I have some sample code, but it's all really basic.

// to open
outfile.open(timestamp_file_name.c_str(),ios::eek:ut | ios::app);

// to close
outfile.flush();
outfile.close();

Can someone help me solve this problem. I've had to make some pretty
interesting side steps to get around this problem before, and now it's
more of a problem to make the work around.

Any help is appreciated,

Kris
 
L

Larry Smith

Admin said:
I have a program that needs to have a couple things happen.

First, the file needs to constantly be updated with new information.
Because the function in which I do this is not always running, I need
to be able to close the file and re-open it.

I know this can be accomplished (in theory) by the following, but for
some reason, I don't have any luck with it. I can't get the program
to open the file, write to it, close it, then re-open it for more file
writing.

I have some sample code, but it's all really basic.

// to open
outfile.open(timestamp_file_name.c_str(),ios::eek:ut | ios::app);

// to close
outfile.flush();
outfile.close();

Can someone help me solve this problem. I've had to make some pretty
interesting side steps to get around this problem before, and now it's
more of a problem to make the work around.

Any help is appreciated,

Kris

This worked for me:

#include <fstream>

int main( )
{
std::eek:fstream out;

out.open("kris.txt", std::ios_base::eek:ut |
std::ios_base::app);

out << "hello\n";

out.flush();
out.close();
// worked for me both with, and without calling clear()
// out.clear();

out.open("kris.txt", std::ios_base::eek:ut |
std::ios_base::app);

out << "bye\n";

out.flush();
out.close();

return 0;
}

I ran it 5 times, and the final 'kris.txt' contained:

hello
bye
hello
bye
hello
bye
hello
bye
 
A

Admin

Larry said:
This worked for me:

#include <fstream>

int main( )
{
std::eek:fstream out;

out.open("kris.txt", std::ios_base::eek:ut |
std::ios_base::app);

out << "hello\n";

out.flush();
out.close();
// worked for me both with, and without calling clear()
// out.clear();

out.open("kris.txt", std::ios_base::eek:ut |
std::ios_base::app);

out << "bye\n";

out.flush();
out.close();

return 0;
}

I ran it 5 times, and the final 'kris.txt' contained:

hello
bye
hello
bye
hello
bye
hello
bye

With the std::ios_base::eek:ut |
std::ios_base::app);
What is the ios_base? I've not seen this before.
 
L

Larry Smith

Admin said:
With the std::ios_base::eek:ut |
What is the ios_base? I've not seen this before.

'ios::eek:ut' and friends pre-date(?) the C++ Std.

'ios_base::eek:ut' and friends are std.

Did you include <fstream>, or did you include the
older (pre-std) <fstream.h> ??
 
L

Larry Smith

Nate said:
Is it necessary to call flush() in this case? I thought close() called
flush() if needed.

Nate

I simply duplicated the OP's code.

cut/paste the code & try it without flush().
 
A

Admin

I actually included the <fstream> then becasue of using strings needed

using namespace std;

could that be part of the problem?

- Kris
 
N

Nate Barney

Larry said:
I simply duplicated the OP's code.

Of course. I didn't mean to say you were doing anything wrong; it was a
poor snip. My apologies if I offended you.
cut/paste the code & try it without flush().

Well, it seems to me that sort of thing could be implementation
dependent, so I asked in hopes of getting a more definitive answer from
one of the regulars on this group.

Nate
 
A

Admin

So I've actually gotten the program to work opening and closing the
file repeatedly
~without~ having using filename.c_str() as the method to get the
filename.

how do I make it so I don't have to use the c_str()?
 
L

Larry Smith

Admin said:
So I've actually gotten the program to work opening and closing the
file repeatedly
~without~ having using filename.c_str() as the method to get the
filename.

how do I make it so I don't have to use the c_str()?

I'm not sure I understand your question...

Do you mean you got the program to work using
'timestamp_file_name.c_str()', and now you want to
know how to get rid of 'timestamp_file_name.c_str()'??

If that is the case, the answer is - you can't.
The open() call requires a 'const char *' (a nul-terminated
C-style string). If 'timestamp_file_name' is a
C++ 'std::string', then its 'c_str()' method must
be invoked to get the 'const char *' req'd by open().

See the documentation for 'basic_ofstream'.
 
D

David Harmon

On 28 Nov 2006 19:26:49 -0800 in comp.lang.c++, "Admin"
So I've actually gotten the program to work opening and closing the
file repeatedly
~without~ having using filename.c_str() as the method to get the
filename.

how do I make it so I don't have to use the c_str()?

As is often said here, "your error is in line 42 of the program."

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[5.8] How do I post a question about code that doesn't work correctly?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 

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
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top