Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > File writing with C++

Reply
Thread Tools

File writing with C++

 
 
Admin
Guest
Posts: n/a
 
      11-29-2006
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: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

 
Reply With Quote
 
 
 
 
Larry Smith
Guest
Posts: n/a
 
      11-29-2006
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: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:fstream out;

out.open("kris.txt", std::ios_base: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: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
 
Reply With Quote
 
 
 
 
Nate Barney
Guest
Posts: n/a
 
      11-29-2006
Larry Smith wrote:
> out.flush();
> out.close();


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

Nate
 
Reply With Quote
 
Admin
Guest
Posts: n/a
 
      11-29-2006

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: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:fstream out;
>
> out.open("kris.txt", std::ios_base: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: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:ut |
> std::ios_base::app);

What is the ios_base? I've not seen this before.

 
Reply With Quote
 
Larry Smith
Guest
Posts: n/a
 
      11-29-2006
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: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:fstream out;
>>
>> out.open("kris.txt", std::ios_base: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: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:ut |
>> std::ios_base::app);

> What is the ios_base? I've not seen this before.
>


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

'ios_base:ut' and friends are std.

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

 
Reply With Quote
 
Larry Smith
Guest
Posts: n/a
 
      11-29-2006
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().

 
Reply With Quote
 
Admin
Guest
Posts: n/a
 
      11-29-2006
I actually included the <fstream> then becasue of using strings needed

using namespace std;

could that be part of the problem?

- Kris

 
Reply With Quote
 
Nate Barney
Guest
Posts: n/a
 
      11-29-2006
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
 
Reply With Quote
 
Admin
Guest
Posts: n/a
 
      11-29-2006
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()?

 
Reply With Quote
 
Larry Smith
Guest
Posts: n/a
 
      11-29-2006
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'.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Any problems with writing the information into a file - Multi-users perform writing the same file at the same time ???? HNguyen ASP .Net 4 12-21-2004 01:53 PM
Question: Writing text file based TestBenches vs. Waveform file based simulation. BLF VHDL 4 08-07-2004 12:44 AM
File Access error - writing to .txt file John Carnahan ASP .Net 2 07-18-2003 10:35 PM
A failure occurred writing to the resources file. Access is denied. -- RESX file is locked? -- WHY? Mark Kamoski ASP .Net 1 07-04-2003 12:02 PM



Advertisments