File writing with C++

Discussion in 'C++' started by Admin, Nov 29, 2006.

  1. Admin

    Admin Guest

    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
    Admin, Nov 29, 2006
    #1
    1. Advertising

  2. Admin

    Larry Smith Guest

    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::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
    Larry Smith, Nov 29, 2006
    #2
    1. Advertising

  3. Admin

    Nate Barney Guest

    Larry Smith wrote:
    > out.flush();
    > out.close();


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

    Nate
    Nate Barney, Nov 29, 2006
    #3
  4. Admin

    Admin Guest

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


    With the std::ios_base::eek:ut |
    > std::ios_base::app);

    What is the ios_base? I've not seen this before.
    Admin, Nov 29, 2006
    #4
  5. Admin

    Larry Smith Guest

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

    >
    > With the std::ios_base::eek:ut |
    >> std::ios_base::app);

    > 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> ??
    Larry Smith, Nov 29, 2006
    #5
  6. Admin

    Larry Smith Guest

    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().
    Larry Smith, Nov 29, 2006
    #6
  7. Admin

    Admin Guest

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

    using namespace std;

    could that be part of the problem?

    - Kris
    Admin, Nov 29, 2006
    #7
  8. Admin

    Nate Barney Guest

    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
    Nate Barney, Nov 29, 2006
    #8
  9. Admin

    Admin Guest

    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, Nov 29, 2006
    #9
  10. Admin

    Larry Smith Guest

    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'.
    Larry Smith, Nov 29, 2006
    #10
  11. Admin

    David Harmon Guest

    On 28 Nov 2006 19:26:49 -0800 in comp.lang.c++, "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()?


    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/
    David Harmon, Nov 29, 2006
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Kamoski
    Replies:
    1
    Views:
    2,441
  2. BLF
    Replies:
    4
    Views:
    1,268
    Mike Treseler
    Aug 7, 2004
  3. John Carnahan

    File Access error - writing to .txt file

    John Carnahan, Jul 18, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    792
    John Carnahan
    Jul 18, 2003
  4. JuneCleaver

    Writing to a log file with in an aspx file.

    JuneCleaver, Apr 8, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,039
    Juan Romero
    Apr 8, 2004
  5. HNguyen
    Replies:
    4
    Views:
    2,388
    HNguyen
    Dec 21, 2004
Loading...

Share This Page