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(ti mestamp_file_na me.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 10 1802
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(ti mestamp_file_na me.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
Larry Smith wrote:
out.flush();
out.close();
Is it necessary to call flush() in this case? I thought close() called
flush() if needed.
Nate
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(ti mestamp_file_na me.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.
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(),io s::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.
'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??
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().
I actually included the <fstreamthen becasue of using strings needed
using namespace std;
could that be part of the problem?
- Kris
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
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()?
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 '. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 problem. The program may crash
unexpectedly while writing to the file. If so, my program should
detect this during startup, and then (during startup) probably delete the
data added to the file and redo the writing operation.
|
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 the capabilities of file.
All works fine, except for one thing: 'print >> myfile' does not
execute Myfile.write(), it executes the file.write(). If I execute
myfile.write() explicitly, then Myfile.write() is called as expected.
|
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 | ios::binary) to declare a file
object with read/write modes turned on for working with binary data.
I've tried this and my file is not created. The only time it is created
is when I specify ifstream or ofstream but not fstream. I've tried
removing the...
|
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
|
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 = tmpfile();
/* write into tmpFile */
...
| |
by: StGo |
last post by:
How can i read/write file's custom attributs(like subject,author...) in
C#???
Thanks :))
|
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 properly (I have a
file that has a size of 1 KB but has 14 bytes in it so the conversion
isn't working right). Is there some method/property out there that
will get the actual size of the file? Also, would there be a method
like this that will...
|
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 by df -k
lopgod10:~/mycrfile # df -k /mnt/mvdg1/vset
Filesystem 1K-blocks Used Available Use% Mounted on
|
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";
if(!chmod($path, 0744)) {
echo "error, $path";
exit;
} else {
|
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 File.Delete() will be sufficient.
Is there anything in .NET that removes any remanence of the file?
If it isn't going to be easy, does anyone know of a component that I can
hook into to do the dirty work, free of otherwise?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |