Can I append text to a file?

Discussion in 'C++' started by gukn9700, Jul 16, 2003.

  1. gukn9700

    gukn9700 Guest

    The following program opens a text file and wants to append new text
    to it, but everytime I run it, it returns me with "open file fail!".
    Can anybody help?


    #include <fstream>
    #include <iostream>
    #include <cstdlib>

    using namespace std;

    int main()
    {
    fstream outfile("test.txt", ios_base::in | ios_base::eek:ut |
    ios_base::app);
    if (!outfile)
    {
    cout << "open file fail!\n";
    exit(1);
    }

    char ch;

    //display original text
    outfile.seekg(0);
    while (outfile.get(ch))
    cout << ch;
    cout << endl;

    //append input text
    outfile.seekp(ios_base::end);
    while (cin.get(ch))
    outfile << ch;

    //display new text
    cout << endl;
    outfile.seekg(0);
    while (outfile.get(ch))
    cout << ch;
    cout << endl;

    system("pause");
    return 0;
    }
     
    gukn9700, Jul 16, 2003
    #1
    1. Advertising

  2. gukn9700

    Jim Fischer Guest

    gukn9700 wrote:
    > The following program opens a text file and wants to append new text
    > to it, but everytime I run it, it returns me with "open file fail!".
    > Can anybody help?
    >
    >
    > #include <fstream>
    > #include <iostream>
    > #include <cstdlib>
    >
    > using namespace std;
    >
    > int main()
    > {
    > fstream outfile("test.txt", ios_base::in | ios_base::eek:ut |
    > ios_base::app);


    This combination of file modes is not allowed; consequently the file
    open attempt fails. FWIW, when specifying append mode, the only legal
    ios_base mode combinations are,

    out | app
    binary | out | app

    [see Table 92 in ISO/IEC 14882:1998]


    > if (!outfile)
    > {
    > cout << "open file fail!\n";
    > exit(1);


    DO NOT use the C library function 'exit()' to terminate a C++ program. I
    realize that C programs commonly use exit() to terminate themselves, but
    using exit() in a C++ program is asking for trouble. To learn the
    specific reasons why you should not use exit() in a C++ program, locate
    any good book / FAQ on the C++ programming language.

    FWIW, if you want/need to respond to error conditions in a C++ program,
    you should use C++'s exception handling mechanism to report/handle these
    conditions. [Research the following C++ keywords: try, catch, throw].

    > }
    > [remainder of code snipped]
    >


    --
    Jim

    To reply by email, remove "link" and change "now.here" to "yahoo"
    jfischer_link5809{at}now.here.com
     
    Jim Fischer, Jul 16, 2003
    #2
    1. Advertising

  3. gukn9700

    gukn9700 Guest

    I know why now!
    everytime a stream reaches its end it can no long input or output or
    even seek position. To make it work we should first clear() it.

    Like:
    outfile.clear();

    That clear the EOF flag and make it work again.

    following code works:

    #include <fstream>
    #include <iostream>

    using namespace std;

    int main()
    {
    fstream outfile("test.txt", ios_base::in | ios_base::eek:ut);
    if (!outfile)
    {
    cout << "open file fail!\n";
    return -1;
    }

    char ch;

    //display original text
    while (outfile.get(ch))
    cout << ch;
    cout << endl;

    //append input text
    outfile.clear(); //must clear before seeking!
    outfile.seekp(0, ios_base::end);
    while (cin.get(ch))
    outfile << ch;

    //display new text
    cout << endl;
    outfile.clear(); //must clear before seeking!
    outfile.seekg(0);
    while (outfile.get(ch))
    cout << ch;
    cout << endl;

    outfile.close();
    system("pause");
    return 0;
    }
     
    gukn9700, Jul 17, 2003
    #3
    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. Dilantha Seneviratne

    How to append to a text file

    Dilantha Seneviratne, Jan 5, 2004, in forum: Java
    Replies:
    10
    Views:
    9,873
    Andrew Thompson
    Jan 9, 2004
  2. Rach
    Replies:
    3
    Views:
    13,578
    Malcolm Dew-Jones
    Aug 24, 2004
  3. sm
    Replies:
    2
    Views:
    545
    Rolf Magnus
    Apr 29, 2005
  4. HYRY
    Replies:
    10
    Views:
    608
    Bruno Desthuilliers
    Sep 26, 2007
  5. sylvaticus
    Replies:
    3
    Views:
    350
    sylvaticus
    Oct 16, 2007
Loading...

Share This Page