473,387 Members | 1,876 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,387 software developers and data experts.

File writing with C++

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::out | 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

Nov 29 '06 #1
10 1788
Admin wrote:
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::out | 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::ofstream out;

out.open("kris.txt", std::ios_base::out |
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::out |
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
Nov 29 '06 #2
Larry Smith wrote:
out.flush();
out.close();
Is it necessary to call flush() in this case? I thought close() called
flush() if needed.

Nate
Nov 29 '06 #3

Larry Smith wrote:
Admin wrote:
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::out | 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::ofstream out;

out.open("kris.txt", std::ios_base::out |
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::out |
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::out |
std::ios_base::app);
What is the ios_base? I've not seen this before.

Nov 29 '06 #4
Admin wrote:
Larry Smith wrote:
>Admin wrote:
>>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::ou t | 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::ofstream out;

out.open("kris.txt", std::ios_base::out |
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::out |
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::out |
> std::ios_base::app);
What is the ios_base? I've not seen this before.
'ios::out' and friends pre-date(?) the C++ Std.

'ios_base::out' and friends are std.

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

Nov 29 '06 #5
Nate Barney wrote:
Larry Smith wrote:
> out.flush();
out.close();

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

Nov 29 '06 #6
I actually included the <fstreamthen becasue of using strings needed

using namespace std;

could that be part of the problem?

- Kris

Nov 29 '06 #7
Larry Smith wrote:
Nate Barney wrote:
>Larry Smith wrote:
>> out.flush();
out.close();
Is it necessary to call flush() in this case? I thought close() called
flush() if needed.

Nate

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
Nov 29 '06 #8
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()?

Nov 29 '06 #9
Admin wrote:
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'.
Nov 29 '06 #10
On 28 Nov 2006 19:26:49 -0800 in comp.lang.c++, "Admin"
<kr***********@gmail.comwrote,
>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/
Nov 29 '06 #11

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

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
2
by: Pierre Rouleau | last post by:
Hi all! I'm trying to extend the functionality of the file object by creating a class that derives from file. MyFile class re-implements __init__(), write(), writelines() and close() to augment...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
8
by: Stewart | last post by:
is there any way this can be done? I've looked at the help files and checked out as many tutorials as i could find on the net (as always) but no joy. thanks
4
by: Oliver Knoll | last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode (that is just writing, right?). How would one reopen it for reading? I got the following (which works): FILE *tmpFile =...
3
by: StGo | last post by:
How can i read/write file's custom attributs(like subject,author...) in C#??? Thanks :))
4
by: Doug | last post by:
Hi, It looks like the only way to get a size of a file within csharp is to use FileInfo and the Length property. However that only returns the number of bytes in the file which is translating...
14
by: prasadjoshi124 | last post by:
Hi All, I am writing a small tool which is supposed to fill the filesystem to a specified percent. For, that I need to read how much the file system is full in percent, like the output given...
3
by: brook | last post by:
hey all - i´m new to php and having trouble writing a simple code which should create a file. here is the most simplified version: <?php $content = "my content"; $path = "test.txt";...
12
by: glennanthonyb | last post by:
Hi The company I work for has finally woken up to data security on our field laptops. I'm writing something in C# that will allow remote deletion of sensitive data and I don't believe...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.