0

C++ beginner here, I am trying to append some text to a pre-written .txt file where every line there is a word. I have been using the method ofstream and ifstream as seen below, but everytime I try to write something, it erases the file. (I am not allowed to use ios:app or simillar)

int append_new_word() {
//First I read everything on the list and save it to a string called Words_in_List

    ifstream data_wordlist_in("woerterliste"); //Opens the txt file
    if (!data_wordlist_in) // checks if the file exists
    {
        cout << "File does not exist!" << endl;
        return 1;
    }

    string Word;
    int line = 0;
    string Vorhandene_Woerter;
    std::getline(data_wordlist_in, Wort);
    do { //line counter, goes through all lines and save it to a string
        line++; 
        std::getline(data_wordlist_in, Word);
        Words_in_List = Words_in_List + "\n" + Word;
        
    } while (!data_wordlist_in.eof());

        cout << Words_in_List << endl;

        data_wordlist_in.close();

    //HEre it should save the string again in the list word per word with the neu appended word

    ofstream data_wordlist_out("woerterliste"); //opens ofstream
        if (!data_wordlist_out)
        {
            cout << "File does not exist!" << endl;
            return 1;
        }
        string new_word_in_list;
        cout << "\n Insert a Word to append: ";
        cin >> new_word_in_list;
        
        data_wordlist_out << Words_in_List << endl << new_word_in_list;

        
    data_wordlist_out.close(); //closes ofstream
}

Everytime I try I open my program it erases the list.

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • 2
    Please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) You can remove some duplicate code and make the reading of the file more robust in quite a trivial way. – Some programmer dude Jan 11 '23 at 14:05
  • As for your problem, here are flags you can use when opening a stream to tell the output stream to *only* append to the file. It will also not truncate the file if it already exists. The truncation is the default option when opening the file. – Some programmer dude Jan 11 '23 at 14:07
  • When you open an ofstream without `ios::app` it's going to erase the existing contents, but your code above first reads the existing contents and then outputs them to the new file. So I don't see what the problem is. – john Jan 11 '23 at 14:16
  • 1
    @Someprogrammerdude The OP does say that (for some reason) that he is not allowed to use `ios::app` – john Jan 11 '23 at 14:17
  • 1
    You will need to simply read the existing file, and write out the new file, from scratch. Do you know how to do that? – Sam Varshavchik Jan 11 '23 at 14:22
  • `Words_in_List` is undeclared in the code above, could you explain where that is declared? – john Jan 11 '23 at 14:23
  • @Gabriel If this is a homework or similar assignment, then please read [How do I ask and answer homework questions?](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) as well as [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems). Always include all requirements *and* limitations in the question itself. – Some programmer dude Jan 11 '23 at 17:03

1 Answers1

0

Your code has some minor problems, but nothing that matches your description of it.

It does line based input, which is strange because nothing in the problem description indicates that reading a line at a time is necessary.

It counts lines, again for no obvious reason.

It skips the first line (maybe this is deliberate, but if so you didn't mention that).

The loop termination is incorrect (see link in the comments).

The function is declared as returning an int but no return is made.

Here some code that addresses these problems. It reads characters not lines (using get()) which makes reading the input simpler, but essentially it's the same technique as your code.

void append_new_word()
{
    string existing_content;
    ifstream in("file.txt");
    char ch;
    while (in.get(ch))
        existing_content += ch;
    in.close();
    cout << "enter a new word ";
    string new_word;
    cin >> new_word;
    ofstream out("file.txt");
    out << existing_content << new_word << '\n';
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Pedantically, depending on filesize, I would pre-allocate `existing_content`'s size to the file's size because character-by-character appends for an entire file would cause *a LOT* of allocations. – Casey Jan 11 '23 at 15:35