Frage

How do I add to an existing text file? The following erases whatever is on the text file. I could do this in C++ but I'm new to C++/CLI.

    public ref class form : public System::Windows::Forms::Form
                {
                public:
                    form(void)
                    {
                        InitializeComponent();
                        void save(String^ word);

                    }
                       //windows form generated code
                        ...
                        ...
                        ...
                        void save(String^ word)
                {
                    StreamWriter^ outFile = gcnew StreamWriter("file.txt");
                    outFile->WriteLine(word);
                    outFile->Close();
                }
        #pragma endregion
             private: System::Void button00_Click(System::Object^  sender, System::EventArgs^  e) {
                         String^ word = "PLOW";
                         save(word);
            }
    };
    }
War es hilfreich?

Lösung

One of the constructors of the StearWriter class has a boolean called "Append". set it to true, this should append the content to the end of the stream instead of overwriting it. basically, all you have to do is replace this code line

StreamWriter^ outFile = gcnew StreamWriter("file.txt");

with this one:

StreamWriter^ outFile = gcnew StreamWriter("file.txt", true);
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top